I know questions similar to this are common. I just don't even know where to begin with rewrite rules with the /'s and .'s I don't know how to retrofit other peoples solutions to mine. Onto my situation:
I am using a basic get on the index.php file that looks like
http://www.example.com/index.php?page=about
I want to rewrite that to
http://www.example.com/about
I know this is fairly simple rewrite wise, but its just a totally different language which I have tried and failed to comprehend. Thanks
Try this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]
Or even:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(?!index.php\b).* index.php?page=$0 [L,QSA]
Note:
You should always set the RewriteBase in an .htaccess file.
I've generalised this so that index.php picks up any string which isn't mapping to a real file
The (?!index.php\b) is a regexp which says "but don't match to index.php"
You will need the [QSA] flag if your requests can contain request parameters. This merges them.
Related
I'm trying to get MapRewrite working for some vanity urls but I'm just not having any luck. I don't get errors, it just doesn't seem to work (redirect).
Here's the code I put in my vhost.conf:
RewriteEngine On
RewriteMap vanURL txt:/var/www/vhosts/myconditions.txt
RewriteCond ${vanURL:$1|not-found} ^(.+)$
RewriteCond %1 ~^not-found$
RewriteRule ^/(.*) /${vanURL:$1|/$1} [L]
What I'm looking to do is to determine if "www.mydomain.com/some_folder" exists. If it doesn't, look in "myconditions.txt" for "some_folder" and redirect to the corresponding location.
Here's an example of MyConditions.txt
some_folder another_folder
some_folder_two another_folder_two
Visiting www.mydomain/some_folder is simply a dead link.
Can anyone point me in the right direction?
(Note that I did test putting garbage in my Vhost.conf and .htaccess to ensure the files are being read)
You cannot use %1 in LSH of the condition, use a negative lookahead like this:
RewriteEngine On
RewriteMap vanURL txt:/var/www/vhosts/myconditions.txt
RewriteRule ^/([^/]+)(/.*)?$ /${vanURL:$1}$2 [PT]
This rewrite rule works fine for domain.com/news but I would like be able to parse multiple levels into variables like domain.com/news/2012/party/zeke etc
I tried experimenting with the regex but failed badly.
RewriteEngine on
RewriteBase /
RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L]
Also tried solutions here:
mod_rewrite with multiple variables
using mod_rewrite to simulate multiple sub-directories
// EDIT: It seems I can just add one rule per level BUT is it possibel to have one rule that does all levels?
RewriteRule ^([^/\.]+)/?$ /page.php?page=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /page.php?page=$1&sub=$2 [L]
You can have recursive rules however all of the parts of your rewrite are data. So how can it be converted to a query string as it does not know how to name the variables.
If you had /section/news/year/2012/user/zeke/ then you could use (NOT TESTED):-
RewriteRule ^section/\w+/([^/]+)/([^/]+)/(.*)$ news/$3?$1=$2 [NC,N,QSA]
RewriteRule ^section/(\w+)/?$ page.php?page=$1 [NC,QSA,L]
This should end up rewriting to `page.php?page=news&year=2012&user=zeke
With what you are trying to do you are going to have to change your requirements and use a version of the above or pass them all down to a page in PHP and do the work in PHP.
Hi Stackoverflow community,
I've been reading through most of the other .htaccess questions, and tried different solutions on my .htaccess file but with no success.
Here's the thing: I have a website (based on CodeIgniter) that already has a good SEO positioning. Now, the customer wants it translated to another language, but I dont want to lose that SEO so I thought of redirecting a URL like http://www.domain.com/contactUs to http://www.domain.com/en/contactUs, being (en) the current positioned language. CI would handle the other language as usual without problem. Im currently doing tests on localhost, so the following rules will have only "domain" instead of "domain.com".
I've tried these Rewrite rules:
RewriteEngine on
Options +FollowSymlinks
RewriteCond ^(.*)/domain/(.*) !^/en/
RewriteRule ^(.*)/domain/(.*)$ $1/domain/en/$2
I want the RewriteCond to filter URLs coming in the new language so that the RewriteRule is not executed. What am I doing wrong?
Do this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/en/ [NC]
RewriteRule ^(.*)$ /en/$1 [L]
You should put all your text in unique language files, then load them like so:
$this->config->set_item('language',"your_language");
$this->lang->load('default', "your_language");
To write out the content, the use:
$this->lang->line('message_id');
More info:
http://codeigniter.com/user_guide/libraries/language.html
This should more efficient than creating a duplicate site while still being SEO friendly. In fact, it should be more SEO friendly.
RewriteCond $2 !^en/
RewriteRule ^(.*)/domain/(.*)$ $1/domain/en/$2
I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]