In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.
mysite/v1.0/foldername
mysite/v1.0/foldername/param1/
mysite/v1.0/foldername/param1/param2/etc
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ foldername.php [QSA,L]
the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.
.htaccess is in foldername
Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.
You are incorrectly rewriting the rules correct rule according to your need would be like,
RewriteEngine On
# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]
# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2¶m2=$3 [L]
Refrences
Reference: mod_rewrite, URL rewriting and "pretty links" explained
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Related
I know this is very common question but I can't seem find any solution regarding in my problem. The folder structure looks like this:
- rootProject
- subfolder
* .htaccess
* index.php
Normal url params: http://website.com/subfolder/?dynamicKey=dynamicValue.
But What I want to achieve is like this: http://website.com/subfolder/dynamicKey/dynamicValue
But I failed to do that, I can only retrieve the dynamicKey.
Here is my current htaccess:
RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
Thanks to #Ar Rakin. Here is the solution.
RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]
With your shown samples and attempted code, please try following htaccess rules. We need to add conditional checks before rewrite rules else later it will affect your existing pages also.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]
NOTE: your used regex [a-z] will only match small letters in uri, in case it needed to check any alphabets(capital OR small) then change FROM [a-z] TO [a-zA-Z] in above rules also.
I'm building a simple API with apache and I want to map /really/long/path/api/captions.php?caption_id=blah to /really/long/path/api/captions/blah. It's important to NOT have to specify a full path in the rewrite rule because I want this to work no matter where I deploy this code to. However, I can't find/figure out a working example of .htaccess rewrite rules that enable me to match based upon only the final part of the extension.
So, assuming that I have, say, captions.php in a dir called api, what .htaccess file do I need to include in api to accomplish this transform without having /really/long/path/ anywhere therein?
(I also want to be able to map /really/long/path/api/captions.php to /really/long/path/api/captions/ and /really/long/path/api/captions.)
I've tried all sorts of wildcard-like syntax; here's one of those non-working attempts:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?captions/(.*?)/?$ /captions.php?caption_id=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /captions\.php\?caption_id=([^\&\ ]+)
RewriteRule ^(.*?)/?captions\.php$ /captions/%1? [L,R=301]
Thanks!
Got there in the end. This is all that was needed:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^captions/?$ captions.php [NC,L]
RewriteRule ^captions/(.*)/?$ captions.php?caption_id=$1 [NC,L]
I am working on a site where I have many short URLs which is redirected to a bigger version of URL. The idea is that the short URLs are distributed offline and users generally remembers them and types in the browser address bar. As example:
http://www.example.com/abc should redirect to http://www.example.com/higher_education/companion/politics/abc.html
Also there are some external links as well. like: http://www.example.com/google will redirect user to https://www.google.com
EDIT -
There is another scenario where http://www.example.com/elementary/def.html will be redirected to http://www.example.com/elementary/xyz.html
However, we now want to achieve this with RewriteMap. The code that we've written for that is as below:
<IfModule rewrite_module>
RewriteEngine on
RewriteMap custommap txt:/var/www/vhosts/example.com/httpdocs/map.txt
#RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule "^(.*)$" "${custommap:$0}" [R,L,NC]
</IfModule>
The map.txt contains:
abc http://www.example.com/higher_education/companion/politics/abc.html
google https://www.google.com
The problem is .htaccss file contains also some RewriteRule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)\.html$ /index.php?sid=$1&ssid=$2 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /index.php?sid=$1 [L,NC,QSA]
So any request comes with .html will be served by index.php as standard CMS feature. However, now the code goes into redirect loop.
If someone shares some light in order to mitigate this issue then that would be great. The Apache version is 2.2 so I can't use any IF-ELSE in that.
Regards,
Aneek
You should put some restrictions on the rule serving URLs from RewriteMap:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${custommap:$0} !^$
RewriteRule ^[^.]+$ ${custommap:$0} [R=301,L,NE]
This will ensure only non-files and non-directories with no DOT in URI will be handled by custommap.
More importantly it will also check existence of a mapping in custommap before redirecting.
Make sure you clear your browser cache before testing this change.
I am trying to redirect a bunch of old blog article URLs using the .htaccess file:
RedirectMatch ^/index\.php/global/article/(.*)$ http://www.mywebsite.com/blog/article/$1
This doesn't really work, however, because my CMS seems to get confused by the index.php bit and keeps adding ?symphony-page= to all the URLs.
It's probably this part that is responsible:
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Can anybody help?
Please try the following (comments included to explain):
RewriteEngine On
# First, redirect the old URIs to the new ones via a 301 redirect.
# This will allow the CMS to take over using the rules that follow.
RewriteRule ^index.php/global/article/(.+)$ /blog/article/$1 [L,R=301]
# Frontend Rewrites - for the CMS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Try this:
#-- Input URL >> http://www.mywebsite.com/index.php/global/article/abc
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} ^/index\.php/global/article/(.*)$
RewriteRule ^.*$ blog/article/%1 [R=301, L]
#--Output URL >> http://www.mywebsite.com/blog/article/abc
I've tested the above with this htaccess.madewithlove and it seems like it will work just fine, hopefully it will work for you too
Screenshot:
I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/