I'm having trouble rewriting the subfolder back to the file.
The redirect works but not the last rule.
Here's what I'm trying to do:
domain.com/file.php?lang=fr
would like to rewrite it back to
domain.com/fr/file.php
no matter what folder the files are in.
I'm only using one language which is french.
RewriteCond %{QUERY_STRING} ^lang=([A-Za-z-]+)/?$
RewriteRule ^(.*)\.php$ %1/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ $1.php?lang=fr&redirect=no [QSA,L]
my current htaccess file:
RewriteEngine On
RewriteBase /domain.com
RewriteCond %{QUERY_STRING} (^|&)lang=([A-Za-z-]+)(/?$|&)
RewriteCond %{QUERY_STRING} !(^|&)redirect=noneed($|&)
RewriteRule ^(.*)\.php$ %1/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ http://localhost/?$1.php?lang=fr&redirect=noneed [QSA,L]
The 1st rule is more powerful then second, so to solve your issue you have to write second at 1st place.
why not use a system like that:
RewriteRule ^(fr|en|es)/(accueil|home|bienvenida)/$ /index.php?lang=$1&act=index[QSA,L]
Try adding the RewriteBase option:
RewriteRule On
RewriteBase /
RewriteCond %{QUERY_STRING} ^lang=([A-Za-z-]+)/?$
RewriteRule ^(.*)\.php$ %1/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ $1.php?lang=fr&redirect=no [QSA,L]
(or if you don't want that option append a / at the beginning of all paths)
Also, your RewriteCond doesn't seem to handle the query string correctly, any additional parameters will cause your first rule to fail. Do this instead (I am still thinking):
RewriteRule On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)lang=([A-Za-z-]+)(/?$|&)
RewriteCond %{QUERY_STRING} !(^|&)redirect=noneed($|&)
RewriteRule ^(.*)\.php$ %2/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ $1.php?lang=fr&redirect=noneed [QSA,L]
Related
I'm using .htaccess for the first time and I'm encountering a loop problem. I'm trying to achieve the following:
http://something.com rewrites to http://something.com/main
http://something.com/anything rewrites to http://something.com/index.php?page=anything
So far my current attempt looks like this, which works satisfactorily:
RewriteEngine on
RewriteBase /
RewriteRule ^/?$ /main [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?page=$1 [NC,L]
However, I would like to remove both rewrite conditions to also allow requests to http://something.com/index.php to become http://something.com/index.php?page=index.php. Removing the two RewriteCond lines results in a loop and the rewrite doesn't work.
What am I doing wrong and how can I fix the problem? Thanks!
You can remove those conditions but you would need to reverse your rules and remove leading / from target URIs:
RewriteEngine on
RewriteBase /
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
RewriteRule ^/?$ main [L]
Also remember that now your first rule will also rewrite css/js/image requests to index.php?page=.... If you want to avoid that then add this condition before first RewriteRule:
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ [NC]
There are many issues related to Apache configuration with mod_rewrite and mod_alias on Stack Exchange but I couldn't find an answer to my issue, so here is another question! ;)
I migrated an old website to a new location.
Articles used to be accessible to an URL such as http://xxx/blog/index.php?post/YYYY/MM/DD/title, so there are many links of that form through the existing webpages.
Now, the URL should be http://xxx/post/YYYY/MM/DD/title, so just remove blog/index.php? from the final URL.
I wanted to use mod_alias and a Redirect clause, but I read here that the Redirect clause was interpreted after the RewriteRule clause, which is a problem in my case because I'm already using a RewriteRule condition.
Here is my current .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
I tried to modify it this way:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/index.php? / [R=301,L]
RewriteRule ^(.*)$ index.php?$1
It almost works except:
http://xxx/blog/index.php?post/YYYY/MM/DD/title is redirected/rewritten to http://xxx/blog/?post/YYYY/MM/DD/title (note the ?) so it doesn't work
it looks like this new line messes up a lot of other URLs that do not start with /blog/index.php? such as the backend URL...
Any help will be more than welcome!
Edit (2014-07-16):
If I use the following rule:
RewriteRule ^/?blog/index\.php(.*)$ /$1 [R=301,L]
then going to
http://xxx/blog/index.php?test
takes me to
http://xxx/?test
which is almost correct (the interrogation mark is still a problem)!
But when I try to match the interrogation mark by adding \?:
RewriteRule ^/?blog/index\.php\?(.*)$ /$1 [R=301,L]
then it just stops working... Going there:
http://xxx/blog/index.php?test
just leaves me there!
Why is that?
Try adding this rule to the beginning of your set of rules:
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
so that it looks like:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
I am working in a project in PHP , I am not using MVC but keeping a single index.php files which changes it's content based on $_GET['module']
My current url format is
http://phpquiz.com/school/index.php?module=user_groups&mid=13&cmid=32
Now i want to change it to
http://phpquiz.com/school/index/user_groups/13/32
I used following script and was able to rewrite url but my site stopped working as it lost get variables.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/school/index.php [NC]
RewriteCond %{QUERY_STRING} ^module=(.*)&mid=(.*)&cmid=(.*)
RewriteRule (.*) http://phpquiz.com/school/%1/%2/%3.asp? [R=301,L]
How can i do this by keeping get vatiables.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/school/index.php [NC]
RewriteCond %{QUERY_STRING} ^module=([^&]+)&mid=([^&]+)&cmid=([^&]+)
RewriteRule ^ http://local.htaccess/school/index.php/%1/%2/%3.asp? [R=301,L]
You seem to be doing reverse. Have this rule:
RewriteEngine On
RewriteRule ^school/index\.php/([^/]+)/([^/]+)/([^/]+)\.asp$ /school/index.php?module=$1&mid=$2&cmid=$3 [NC,QSA,L]
I want to be able to handle mod rewrites from within PHP instead of my .htaccess file, this way when I have custom modules that need a new rewrite I don't have to redo the .htaccess file.
I want to mimic the way that WordPress does their .htaccess which is:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
My current .htaccess is this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# MBP Rules
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&page=$3 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&id=$3 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/?$ /index.php?p=$1 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=$1&s=$2 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=$1&s=$2&id=$3 [NC,QSA,L]
Does anyone know how to make this happen?
My way is to replace
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
With
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
Then you have to do similar parsing and everything as modrewrite does, but you can do it yourself using preg_match on $_GET['path']. I.e.
if (preg_match('/id/([0-9]*)', $_GET['path'], $matches)) {
Do code;
}
Sure, have the .htaccess file look like the first example you have, and build your script from there.
Probably write some sort of Router class, to route the requests to their places, the fundamentals being splitting the received query by /, which gives you an array of URL parts, and start conditioning.
I have an url which is http://www.urlbookmarking.com/bookmarks-details.php?bid=55
and I want it to be like
http://www.urlbookmarking.com/bookmark/55
I wrote in my htaccess:
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
But when I go to the first URL the rewrite engine does not apply my rule. Is there any mistake, or conflict somewhere?
My full htaccess file written as follows
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
Please help me.
The line Options +FollowSymLinks is optional if already configured in httpd.conf
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking\.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [R=301, L]
RewriteRule ^bookmark/([0-9]+)$ bookmarks-details.php?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things:
RewriteEngine On only needs to called once, though this may not be causing any problems
I also have RewriteBase / after my RewriteEngine On line
My rewrite rule looks like this: RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L], which tells me that your rule should looke like this RewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
you should use only one RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1 - put this line after Options +FollowSymLinks
Try again
Do you want you url to be /bid/55 or /bookmark/55? because you have written it as if it is going to be /bid/55...
Anyway, your .htaccess should look more like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^bid/(.*)$ bookmarks-details.php?bid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
...without the multiple RewriteEngine on directives (these don't break anything but are unnecessary), and without the leading forward slash on the bid rewrite rule. Also, put your new rule before the rules that rewrites for non-existent file so it doesn't rewrite your URL before you get a chance to use it, and add a [L] flag to the rule so it doesn't get further modified by the other rules. Also, add the line start/end markers (^/$) to the rule.
You would only use the leading forward slash if you were putting the rules in httpd.conf, you don't use them in .htaccess files.
If you want your urls to be /bookmark/, just replace bid with bookmark.
This should redirect all '/bookmarks-details.php\?bid=(id)' urls with bookmarks ids (that have only numbers) to /bookmark/(id).
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
Once you successfully rewritten the URL, you then need to write a companion rule to process it, like so:
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
If should go between the rule that always adds 'www' to the beginning and the catch all rule, which I placed at the end. All together, it may look like so:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This link may make things clearer: http://corz.org/serv/tricks/htaccess2.php