I'm trying to do that:
For example, I've got website:
http://web.com
and when I enter
http://web.com/text
website will be the same but <h1></h1> will change to <h1>text</h1>
when I enter
http://web.com/title
website will be the same but <h1></h1> will change to <h1>title</h1> etc.
One way to do this.
In your .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/file.html$
RewriteRule .* /file.html [L,R=302]
In file.html
<h1 id="header">text</h1>
<script type="text/javascript">
if (window.location.pathname != "/") {
document.getElementById("header").innerHTML = window.location.pathname;
}
</script>
Related
user friendly URL in yii2 not working. i can't figure out why.... here is my code from project
eshop/.htaccess
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
AddDefaultCharset utf-8
RewriteCond %{REQUEST_URL} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ /web/css/$1 [L]
RewriteRule ^fonts/(.*)$ /web/fonts/$1 [L]
RewriteRule ^js/(.*)$ /web/js/$1 [L]
RewriteRule ^images/(.*)$ /web/images/$1 [L]
RewriteRule (.*)$ /web/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
eshop/web/.htaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
eshop/config/web
....
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'category/<id:\d+>/page/<page:\d+>' => 'category/view',
'category/<id:\d+>' => 'category/view',
'product/<id:\d+>' => 'product/view',
'search' => 'category/search',
],
],
...
it's working without user friendly URL http://yii2.loc/eshop/web/index.php?r=category%2Fview&id=4 but when i use user friendly URL, url become like this http://yii2.loc/eshop/web/category/4 but it's not working. I just got Object not found! help me please
I need some help with UrlManager in Yii2.
I have blog application. There is module called 'blog'. There is BlogController with index action. To run it i must run url like this localhost/myblog/web/blog/blog/index
and
localhost/myblog/web/blog/blog/post?id=4 (for action post).
My settings in web.php are like this:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
//'enableStrictParsing' => true,
'rules' => [
//'<action:(blog|index|)>' => 'blog/blog/index',
//'/' => 'blog/blog/index'
]
],
I have tried run with line that commented but with no success.
How to configure it to have urls like this localhost/myblog/web/index and localhost/myblog/web/post?id=4 ?
Regards.
"urlManager" alone can not help you. You need to use .htaccess. Then it will work how you want. Please copy bellow codes in you .htaccess. The .htaccess you should put in your root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
Hope this will help. Thanks.
I have both category and subcategory like following
main category is like this www.example.com/cat/11
sub category is also like this www.example.com/cat/20
i want category like this :
www.example.com/category-name/
and subcategory like :
www.example.com/category-name/subcategory-name
how can i define rules for main category and sub category and get it into this URL structure?
i have this .htaccess file now
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?cat=$1&cat=$2 [L]
Edited:
After your question i guess you want not only 2 levels but more.
With this htaccess all requests will be handled by the index.php
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]
In you index php you can parse the request uri, here an example
<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
//if not in RootDir
$baseDirectory = '/';
$categories = explode('/', substr($path, strlen($baseDirectory)));
var_dump($categories);
//will be ['category'] for domain.tld/category
//will be ['category', 'subcategory'] for domain.tld/category/subcategory
//will be ['category', 'subcategory', 'more'] for domain.tld/category/subcategory/more
I think that is what you need.
I need help with Rewrite issues.
I believe this is the line causing the problem.
#define listing
RewriteRule ^([^/][\w\-\_]+)(.+)-l?([0-9]+).ht(m|ml)$ index.php?page=$1&rlVareables=$2&listing_id=$3 [QSA,L]
when I visit this page http://qvoo.no-ip.org/listings/clothing-and-shoes/children-s-clothes/cute-korea-cartoon-character-kids-adult-socks-make-1.html
it shows me The page isn't redirecting properly
additional info
PHP Version 5.5.15
Apache 2.2
Full code
# enable apache morRewrite module #
RewriteEngine on
RewriteBase /
# set files headers
<IfModule mod_headers.c>
<FilesMatch "\.(css|js|png|gif|jpg|jpeg|htc)$">
Header set Cache-Control "max-age=2678400, public, must-revalidate"
</FilesMatch>
</IfModule>
# deflate static content
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/x-component text/css application/x-javascript application/javascript
</IfModule>
# allow request methods
<Limit POST PUT DELETE GET OPTIONS HEAD>
Order deny,allow
Allow from All
</Limit>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
ErrorDocument 404 /404.html
# define system languages
RewriteRule ^([^//]+)/?(.{2})?/$ index.php?page=$1 [QSA,L]
# define paging
RewriteRule ^([^//]+)/?(.*)?/index([0-9]*).ht(m?ml?)$ index.php?page=$1&rlVareables=$2&pg=$3 [QSA,L]
# define listing
RewriteRule ^([^/][\w\-\_]+)(.+)-l?([0-9]+).ht(m|ml)$ index.php?page=$1&rlVareables=$2&listing_id=$3 [QSA,L]
# define account details request
RewriteRule ^((\w{2})/)?([\w-_]{3,})$ index.php?page=$3&lang=$2&account_request [QSA,L]
# define single pages
RewriteRule ^([^//]+)/?(^/*)?.ht(m?ml?)$ index.php?page=$1 [QSA,L]
# define other pages
RewriteRule ^([^//]+)/?(.*)?/?(.*)?(.ht(m?ml?)|/+)$ index.php?page=$1&rlVareables=$2 [QSA,L]
Hello guys i have problem in ZF2 with htaccess.
I create vhost and all work good. when i call
my-vhost.localhost all work good but when i add some uri segment like /index.php or /1234
I get 404.
I edit .htaccess in public dir and put
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [NC,L]
Bur again i have 404. Any idea how i can i slow this?
My full .htaccess is :
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
VHOST
<VirtualHost *:80>
ServerName zf2-tutorial.localhost
DocumentRoot /var/www/html/zf2-tutorial/public
SetEnv APPLICATION_ENV "development"
<Directory /var/www/html/zf2-tutorial/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Who think problem is routing check my route mod.
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
The contents of the shipped .htaccess file are as follows;
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
It differs very slightly from your copy, I have tried your .htaccess file on a working example and it appears fine.
Is the 404 you are getting a generic "server looking" 404 or is it a ZF2 themed page?
Might be a silly suggestion but have you tried http://my-vhost.localhost/public or http://my-vhost.localhost/public/album ???
As far as I know you shouldnt need to call .php files directly
Im not sure it is too late to answer this question, as Finbarr suggest to try with
http://my-vhost.localhost/public or http://my-vhost.localhost/public/album.
I had also the same problem everytime I type www.zf2-tutorial.loc it always was redirected to xampp main page.
Finally i read the comments froom Finbarr i changed to
http://www.zf2-tutorial.loc/zf2-tutorial/public/
after that the webspage: Welcome to Zend Framework 2 was loaded.
#the virtual host:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
<DocumentRoot "C:/xampp/htdocs/zf2-tutorial/public"
ServerName www.zf2-tutorial.loc zf2-tutorial.loc
ServerAlias zf2-tutorial.loc
<Directory "C:/xampp/htdocs/zf2-tutorial/public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
in the host I made several combinations trying to find out which one was correct.
127.0.0.1 localhost
127.0.0.1 www.zf2-tutorial.loc zf2-tutorial.loc
127.0.0.1 www.zf2-tutorial.loc zf2-tutorial.loc localhost
127.0.0.1 www.zf2-tutorial.loc localhost
127.0.0.1 www.zf2-tutorial.loc
127.0.0.1 zf2-tutorial.loc localhost
127.0.0.1 zf2-tutorial.loc
the apache (httpd conf) has this settings some people suggest to delete he hash tag but I didn't work for me so left it as i found it:
# Virtual hosts
# Include conf/extra/httpd-vhosts.conf
Also in the same file (httpd conf) I change the AllowOverride None to AllowOverride FileInfo as follows:
<Directory />
AllowOverride FileInfo
Require all denied
</Directory>
re started the xampp and it was displayed the congratulations web page.