This is might be a classic .htaccess question, but still I couldn't find the question for my specific case. Here's the closest I found (parameters involve in my case). Done implementing the two answers. Couldn't work for my case.
My case
1) I want any access for this URL,
mywebsite.com/any-first-segment?param1=a¶m2=b¶m3=c¶mN=anything
will be remapped to
mywebsite.com/index.php?p=any-first-segment¶m1=a¶m2=b¶m3=c¶mN=anything
2) Still working if there's no param.
3) I will never have more than one segment. (zero segment still works, mapped to index.php as usual)
Could you suggest me the working RewriteRule for this case?
Here's the last .htaccess, which is still not working
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?p=$1 [QSA,L]
[Updated!]
It's working. I found I messed up with Options +FollowSymLinks after reading this
Here's the working .htaccess. With exception.
RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.+)$ index.php?p=$1 [QSA,L]
Using the QSA flag in your rule will append the current query string to the rewritten form.
Related
This is might be a classic .htaccess question, but still I couldn't find the question for my specific case. Here's the closest I found (parameters involve in my case). Done implementing the two answers. Couldn't work for my case.
My case
1) I want any access for this URL,
mywebsite.com/any-first-segment?param1=a¶m2=b¶m3=c¶mN=anything
will be remapped to
mywebsite.com/index.php?p=any-first-segment¶m1=a¶m2=b¶m3=c¶mN=anything
2) Still working if there's no param.
3) I will never have more than one segment. (zero segment still works, mapped to index.php as usual)
Could you suggest me the working RewriteRule for this case?
Here's the last .htaccess, which is still not working
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?p=$1 [QSA,L]
[Updated!]
It's working. I found I messed up with Options +FollowSymLinks after reading this
Here's the working .htaccess. With exception.
RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.+)$ index.php?p=$1 [QSA,L]
Using the QSA flag in your rule will append the current query string to the rewritten form.
I'm trying to rewrite URLs for my dynamically generated PHP site.
I load new templates into index.php by using the following GET:
localhost/dmk/?req=signin
localhost/dmk/?req=useraccount
I want these links to appear as:
localhost/dmk/signin
localhost/dmk/useraccount
But for the life of me I cannot figure out how to do this. Everything I try either produces a 500 Internal Server Error, or has no effect at all.
I must be missing the point of RewriteRule.
You should read some documentation in this direction. I know it's a bit frustrating at first to write the rules, but it gets easier. You need to learn regular expressions to write the rules (you can start here: http://www.regular-expressions.info/)
As for the rules you need, they go like this:
RewriteEngine On
RewriteRule ^signin$ index.php?req=signin [L,QSA]
RewriteRule ^useraccount$ index.php?req=useraccount [L,QSA]
or
RewriteRule ^(signin|useraccount)$ index.php?res=$1 [L,QSA]
You can paste the rules you have used, maybe someone will explain you what you did wrong.
Try this
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^dmk/(.+)$ dmk/?req=$1 [NC,QSA,L]
This would redirect any URL like /dmk/page that does not conflict with an existing file or directory to /dmk/?req=page. I'm assuming your index.php is in /dmk directory.
I am looking for an .htaccess rule for the following situation. I have found fragments of what I am looking for on this site, but I don't have the experience to put them together correctly. The .htaccess rule would process the following scenarios.
The first two examples already work by default with DirectoryIndex, but I included them to make sure they would still work.
/
-> /index.php
/home
-> /home/index.php
/home/hello
-> /home/hello.php
/home/hello/there
-> /home/hello.php?var1=there
/home/hello/there/again
-> /home/hello.php?var1=there&var2=again
Additionally, a trailing / must be optional for each scenario.
Lastly, requests for images or other files should still process correctly. If required, I could put everything that doesn't follow this rule in a folder such as /assets.
So far,
RewriteRule ^([^/]+)/([^/]+)/? /$1/$2.php [NC]
works for /home/hello -> /home/hello.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /$1/$2.php?var1=$3 [NC]
works for /home/hello/there -> /home/hello.php?var1=there and
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /$1/$2.php?var1=$3&var2=$4 [NC]
works for /home/hello/there/again -> /home/hello.php?var1=there&var2=again
but I have yet to get them to play well together and to also omit a directory for images and other assets.
Thank you very much for any recommendations.
Options -MultiViews
RewriteEngine On
RewriteBase /
DirectoryIndex index.php
RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.*?)/(\w+)/(.*?)/(.*?)/? $1/$2.php?var1=$3&var2=$4 [L,QSA]
RewriteRule ^(.*?)/(\w+)/(.*?)/? $1/$2.php?var1=$3 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*?)/(\w+)/? $1/$2.php [L]
Per Directory evaluation is iterative so you need a stop criteria. This is what rule 1 does.
The Directory index directive handles the index.php additions.
Rule 2-4 do your SEO to Get mapping. Note that I've changed parameter 2 to match any word string.
.+? is a non-greedy match. It will stop at the first /
I think I would create different rules myself. In inverse order of what you have listed to capture. So if you can capture 2 vars do that first, then try 1 var, then just the page etc.
Here is something that may be of more help http://martinmelin.se/rewrite-rule-tester/
Currently my .htaccess looks like this...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It currently changes any /xxx.php file into /xxx. This is great for SEO. However, I also want Mr. htaccess to convert certain URLs into a URL + query string. For instance when user goes to
/specific/somerandominfo
Then somerandominfo is passed to the specific.php file. I normally have no problem doing this using rewrites, but because of my fancy catchall rewrite, I can't figure out how to do it.
For example if I add
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
to my .htaccess, then hitting up /specific/somerandominfo just serves me a big fat 500 Internal Service Error.
Any help from you apache gurus out there would be so, so cool.
Thanks!
p.s. anybody want to also throw in any other cool SEO tricks that they like? I'll bake you cookies.
You are getting 500 error because your rules are creating an infinite cycle. Check apache error log to see if it is true. So you should design your rules properly. Maybe like that:
RewriteRule ^([^/]*)$ $1.php [L]
RewriteRule ^(.*)/(.*)$ $1.php?var=$2 [L]
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
This is mostly correct. I'd just add the B flag, like this:
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC,B]
This causes the capture group $1 to be properly escaped for use in query strings. Note that you can still use QSA to retain the query parameters used in the original request (in addition to somerandominfo).
Perhaps you'll want to post your actual RewriteRule.
I want to redirect URLs from an old site that used raw URL requests to my new site which I have implemented in CodeIgniter. I simply want to redirect them to my index page. I also would like to get rid of "index.php" in my URLs so that my URLs can be as simple as example.com/this/that. So, this is the .htaccess file I have created:
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond $1 ^assets
RewriteRule ^(.*)$ example/production/$1
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ index.php? [R=301]
RewriteCond $1 !^(index\.php|example|robots\.txt)
RewriteRule ^(.*)$ index.php/$1
It should also be noted that my index.php is actually a symlink to example/production/index.php.
Now, the first rule works as expected - all my styles and images show up just fine, it's the second two rules I'm having trouble with. The second rule is basically to destroy the query string and redirect to my index page (externally). So, I found this in the Apache manual:
Note: Query String
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
However, when I try to access one of the old pages, instead of redirecting to my index page, I get a 404 page not found error. I have figured out a workaround by making it an internal redirect, but I would really like it to be external.
The next problem, and the one that has been baffling me the most is with the third rule. I would expect this to do something like the following. If I type in:
http://example.com/this/thing
I would expect it to re-route to
http://example.com/index.php/this/thing
Unfortunately, this does not work. Instead, no matter what I type in, it always routes to my index page as if nothing else was in the URL (it just goes to http://example.com/).
Furthermore, and even more confusing to me, if I replace that rule with the following:
RewriteCond $1 !^(index\.php|example|robots\.txt)
RewriteRule ^(.*)$ index.php/this/thing
If I type in a URL such as http://example.com/other/thing, then it will go to http://example.com/index.php/this/thing as expected, BUT if I type in http://example.com/this/thing it goes to http://example.com/ (my index page). I can't make heads or tails out of it. Any help would be greatly appreciated.
This should solve your index.php problem and it will simply detect if a robots.txt is available:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
hmmm - this doesn't seem to work either. The problem is my URLs aren't really asking for a filename or directory anyway. For example: example.com/index.php/this/thing should call the 'thing' method of the 'this' controller. – Steven Oxley
The condition is: If request is NOT a file and NOT a directory, so that was right, what you should have done is combine the appending of the request string:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]