I am working in yii framework and try to rewrite url using htaccess file
this is my file
(at first step I am trying to work with a simple rule)
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^carguide$ MainModel/index/304 [NC,L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I want to rewrite url carguide to mainModel/index/304,
but no meter what i tried the yii receive the orliginal url and raise 404 error.
here is the log file:
strip per-dir prefix: C:/xampp/htdocs/cars/carguide -> carguide
applying pattern '^carguide$' to uri 'carguide'
RewriteCond: input='C:/xampp/htdocs/cars/carguide' pattern='!-f' => matched
RewriteCond: input='C:/xampp/htdocs/cars/carguide' pattern='!-d' => matched
rewrite 'carguide' -> 'MainModel/index/304'
add per-dir prefix: MainModel/index/304 -> C:/xampp/htdocs/cars/MainModel/index/304
strip document_root prefix: C:/xampp/htdocs/cars/MainModel/index/304 -> /cars/MainModel/index/304
internal redirect with /cars/MainModel/index/304 [INTERNAL REDIRECT]
add path info postfix: C:/xampp/htdocs/cars/MainModel -> C:/xampp/htdocs/cars/MainModel/index/304
strip per-dir prefix: C:/xampp/htdocs/cars/MainModel/index/304 -> MainModel/index/304
applying pattern '^carguide1$' to uri 'MainModel/index/304'
add path info postfix: C:/xampp/htdocs/cars/MainModel -> C:/xampp/htdocs/cars/MainModel/index/304
strip per-dir prefix: C:/xampp/htdocs/cars/MainModel/index/304 -> MainModel/index/304
applying pattern '.' to uri 'MainModel/index/304'
RewriteCond: input='C:/xampp/htdocs/cars/MainModel' pattern='!-f' => matched
RewriteCond: input='C:/xampp/htdocs/cars/MainModel' pattern='!-d' => matched
rewrite 'MainModel/index/304' -> 'index.php'
add per-dir prefix: index.php -> C:/xampp/htdocs/cars/index.php
strip document_root prefix: C:/xampp/htdocs/cars/index.php -> /cars/index.php
internal redirect with /cars/index.php [INTERNAL REDIRECT]
strip per-dir prefix: C:/xampp/htdocs/cars/index.php -> index.php
applying pattern '^carguide$' to uri 'index.php'
strip per-dir prefix: C:/xampp/htdocs/cars/index.php -> index.php
applying pattern '.' to uri 'index.php'
RewriteCond: input='C:/xampp/htdocs/cars/index.php' pattern='!-f' => not-matched
pass through C:/xampp/htdocs/cars/index.php
I tried to change the flags, add a RewiteBase.
As I can see you don't need to use .htaccess to create a particular rewrite rule for each page of your site. It's enough to use simple .htaccess as proposed here and then configure your rewrite rules using Yii's URL manager.
UPDATE
If your controller's name is mainModel and your action's name is index (in terms of Yii the route is mainModel/index) and you want this page to be accessable through http://yoursite/carguide then your configuration of URL manager in protected/config/main.php could look like this:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'carguide' => 'mainModel/index',
)
),
UPDATE
Please refer to this page for configuration properties supported by Yii's URL manager class.
Related
I am having the following setup:
Domain -> Cloudfront -> ELB -> #instances
This fails ONLY (!) on the index page, all sub pages are working fine!
If I remove the Cloudfront in the chain, everything works.
So the server error says "Too many redirects". I am using currently (for testing) the following .htaccess content ("official" example):
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php
# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks
# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
# to the front controller "/index.php" but be rewritten to "/index.php/index".
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/index.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 307 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
I enabled trace logging for htaccess mod rewrite with the following output if I open the index page with cloudfront.domain.com
[rewrite:trace2] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff233f70/initial] [perdir /var/www/html/web/] trying to replace context docroot /var/www/html/web with context prefixerror_log
[rewrite:trace1] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff233f70/initial] [perdir /var/www/html/web/] internal redirect with /index.php [INTERNAL REDIRECT]error_log
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] strip per-dir prefix: /var/www/html/web/index.php -> index.php
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] applying pattern '^(.*)' to uri 'index.php'
[rewrite:trace4] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] RewriteCond: input='/index.php::index.php' pattern='^(/.+)/(.*)::\\\\2$' => not-matched
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] strip per-dir prefix: /var/www/html/web/index.php -> index.phperror_log
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] applying pattern '^' to uri 'index.php'
[rewrite:trace4] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] RewriteCond: input='' pattern='.' => not-matched
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] strip per-dir prefix: /var/www/html/web/index.php -> index.php
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] applying pattern '^index\\\\.php(?:/(.*)|$)' to uri 'index.php'
[rewrite:trace4] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] RewriteCond: input='200' pattern='^$' => not-matched
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] strip per-dir prefix: /var/www/html/web/index.php -> index.php
[rewrite:trace3] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] applying pattern '^' to uri 'index.php'
[rewrite:trace4] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] RewriteCond: input='/var/www/html/web/index.php' pattern='-f' => matched
[rewrite:trace1] ... [cloudfront.domain.com/sid#55e9fedd93e0][rid#55e9ff166a68/initial/redir#1] [perdir /var/www/html/web/] pass through /var/www/html/web/index.php
The comments in the .htaccess files say that you should comment the lines if the error with "too many redirects" occurs. But this is not working for me. Cause the problem only occurs with cloudfront in front of the webserver.
Also this setup was working fine with Symfony 3.4!
Any .htaccess server gurus out there who can tell me what is possibly causing the problem?
I fixed it by myself.
The problem seems to be that Cloudfront is adding a slash to the REQUEST_URI, which leads to a duplicate / on the index page and a redirect to itself and so on.
So my hotfix was to put that at the beginning of the index.php file to remove // from the REQUEST_URI:
$_SERVER['REQUEST_URI'] = '/' . ltrim(#$_SERVER['REQUEST_URI'], '/');
I have a PHP application that uses GET variables to determine language, that is:
http://example.com/?lang=en
And I'm writing a mod_rewrite rule so that people can access it using SEO friendly URLs:
http://example.com/en/
My .htaccess looks like this so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[a-z]{2} /test.php?lang=$0 [L]
(The test.php thing is just a test, not the name of my real app).
But when I access:
http://example.com/fr/blabla
What I get in my rewrite_log is the following:
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) [perdir /var/www/html/example/] add path info postfix: /var/www/html/example/fr -> /var/www/html/example/fr/blabla
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) [perdir /var/www/html/example/] strip per-dir prefix: /var/www/html/example/fr/blabla -> fr/blabla
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) [perdir /var/www/html/example/] applying pattern '^[a-z]{2}' to uri 'fr/blabla'
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (2) [perdir /var/www/html/example/] rewrite 'fr/blabla' -> '/test.php?lang=fr'
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) split uri=/test.php?lang=fr -> uri=/test.php, args=lang=fr
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (1) [perdir /var/www/html/example/] internal redirect with /test.php [INTERNAL REDIRECT]
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b92310/initial/redir#1] (3) [perdir /var/www/html/example/] strip per-dir prefix: /var/www/html/example/test.php -> test.php
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b92310/initial/redir#1] (3) [perdir /var/www/html/example/] applying pattern '^[a-z]{2}' to uri 'test.php'
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b92310/initial/redir#1] (1) [perdir /var/www/html/example/] pass through /var/www/html/example/test.php
What I don't understand is why, after the first match (4th line in the log), mod_rewrite keeps matching and redirecting again and again. I found several people posting similar questions here, and as a result of their answers, I tried adding the RewriteCond rule, but mod_rewrite seems to ignore it (yes, test.php does exist in my filesystem). What's going on?
EDIT: forgot to mention. The server I'm testing this on is an Apache 2.2.3.
EDIT: nevermind. It was a typo in my test.php, which was preventing it from being executed correctly (can you add here one of those "eyeroll" or "embarrassed" emojis?)
To stop after first rewrite you can use:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[a-z]{2} test.php?lang=$0 [L,QSA]
%{ENV:REDIRECT_STATUS} is internal variable that is set to 200 after first rewrite by Apache.
However RewriteCond %{REQUEST_FILENAME} !-f should stop rewrite loop after first rewrite if test.php is a valid file.
I am moving my site from Mediawiki to Wordpress and would like to redirect this page:
http://wecheck.org/wiki/Aaron_Swartz
to this page:
http://newslines.org/wiki/category/computer-people/aaron-swartz/
Currently in .htaccess I have
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^title=Aaron_Swartz$
RewriteRule ^/w/index\.php$ http://newslines.org/wiki/category/computer-people/aaron-swartz/? [L,R=301]
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
RewriteRule ^/?$ %{DOCUMENT_ROOT}/w/index.php [L]
The second part makes the pretty URLs for mediawiki. I have tried many, many variations but I can't get it to work at all. Any help most appreciated.
UPDATE: Log file using the solution given. What is the .phtml?
[24/Jan/2013:22:01:00 +0000] init rewrite engine with requested uri /wiki/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (1) pass through /wiki/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (1) [perdir /var/www/] pass through /var/www/w/wiki.phtml
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] add path info postfix: /var/www/w/wiki.phtml -> /var/www/w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] strip per-dir prefix: /var/www/w/wiki.phtml/Aaron_Swartz -> w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] applying pattern '^wiki/Aaron_Swartz$' to uri 'w/wiki.phtml/Aaron_Swartz'
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] add path info postfix: /var/www/w/wiki.phtml -> /var/www/w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] strip per-dir prefix: /var/www/w/wiki.phtml/Aaron_Swartz -> w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] applying pattern '^w/index\.php$' to uri 'w/wiki.phtml/Aaron_Swartz'
[24/Jan/2013:22:01:00 +0000] (1) [perdir /var/www/] pass through /var/www/w/wiki.phtml
Remember that Apache directives like RewriteRule are applied before MediaWiki even sees the request. Thus, your current rule should work for http://wecheck.org/w/index.php?title=Aaron_Swartz, but not for http://wecheck.org/wiki/Aaron_Swartz.
Actually, though, the rule won't work because your regexp begins with a /, but in .htaccess context the leading slash (or whatever you've set RewriteBase to) is removed before the rewrite rules are applied.
Thus, fixing those two problems, what you need is something like this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# match the short URL of the page:
RewriteRule ^wiki/Aaron_Swartz$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]
# optional: also match the long version of the URL:
RewriteCond %{QUERY_STRING} ^title=Aaron_Swartz$
RewriteRule ^w/index\.php$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]
Edit: Based on your log file, it looks like you have a wiki.phtml file in your webserver root, to which Apache is automatically resolving any URL paths beginning with /wiki/.
One workaround would be to move your rewrite rules to the main Apache config where they'll run before any such mapping is done; another, more straightforward way would be to just change the first rewrite rule above to:
# match the short URL of the page:
RewriteRule ^wiki\.phtml/Aaron_Swartz$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]
or even:
# match the short URL of the page:
RewriteRule ^wiki(\.phtml)?/Aaron_Swartz$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]
I have a bit of problem, here my log:
1. strip per-dir prefix: C:/wamp/www/web.site/projects/test/ -> projects/test/
2. applying pattern '(.*)' to uri 'projects/test/'
3. RewriteCond: input='C:/wamp/www/web.site/projects/test/' pattern='!-f' => matched
4. RewriteCond: input='C:/wamp/www/web.site/projects/test//index.php' pattern='!-f' => not-matched
5. pass through C:/wamp/www/web.site/projects/test/
6. strip per-dir prefix: C:/wamp/www/web.site/projects/test/index.php -> projects/test/index.php
7. applying pattern '(.*)' to uri 'projects/test/index.php'
8. RewriteCond: input='C:/wamp/www/web.site/projects/test/index.php' pattern='!-f' => not-matched
9. pass through C:/wamp/www/web.site/projects/test/index.php
.
Question is, why wont it stop at Line 5? or why is line 9 not line 5 ? Is there a way to prevent this? Here the code i have in my .htaccess file.
.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule (.*) index.php [NC,L]
.
Can some one help me out here? I wan't faster loading times. Thank you in advance.
It seems that you've configured Apache to a simple thing: if there's a directory that is requested then try to serve "index.php" that is in this directory:
DirectoryIndex index.php
So here's what's happening: Apache rewrites everything fine, then sees it's a directory => it tries to server index.php. The behavior seems both normal and pretty common ;)
Want to forward the following:
localhost/bla/index.html -> localhost/index.php?s=bla/index.html
localhost/blub.html -> localhost/index.php?s=blub.html
localhost/blog.html?site=10 -> localhost/index.php?s=blog.html&site=10
localhost/folder -> localhost/index.php?s=folder
but I only want do redirect .html and folders.
RewriteCond %{REQUEST_URI} .html$ [OR]
RewriteCond -d
RewriteRule (.*) /index.php?s=$1 [L,QSA]
Only if the uri ends with ".html" or if it's a folder, it rewrites the entire uri into the ?s= query string. The QSA flag should make sure that query strings on .html files (?site=10) are added to the new url as well.