I'm not too inexperienced with ReWrite (not a master either, though) so I was hoping somone might be able to help me.
RewriteRule ^$ index.php?page=home [NC]
RewriteRule ^adm$ index.php?page=adm_home [NC]
RewriteRule ^adm/stats index.php?page=adm_stats [NC]
Above is a snippet of my .htaccess file. As you can see, when someone visits http://www.example.com/adirectory/ it actually calls on index.php?page=home, similarly if someone goes to http://www.example.com/adirectory/adm/ it will still call index.php?page=adm_home within the "adirectory".
What I'm wanting to achieve is this: I want to be able to display alerts on my pages, and to do this I want to simply be able to add alert=n (where n is a number) and thus have the redirect as index.php?page=home&alert=n
However, I can't understand how this can be done, regex is confusing me. Seeking your help.
You can set the QSA flag to automatically append the originally requested query string to the new one:
RewriteRule ^$ index.php?page=home [L,QSA]
RewriteRule ^adm$ index.php?page=adm_home [L,QSA]
RewriteRule ^adm/stats$ index.php?page=adm_stats [L,QSA]
Related
I've been searching and cannot find an answer that suits my needs. It is a rather simple question I assume.
The point is that I need to rewrite my files to a name of my liking. For example I have, 'www.mydomain.com/account_index.php' which I want to rewrite to www.mydomain.com/account
Thus far I had this, but that doesn't seem to work with multiple lines.
Rewriteengine on
rewriterule ^support account_ticket.php [L]
rewriterule ^account account_index.php [L]
So in short, I want my website to redirect /account to account_index.php and the other way around.
Thanks in advance!
I found the answer for those that are wondering.
I had to put a RewriteCond before every RewriteRule.
So if I wanted to go to www.mydomain.com/account. I'd have this:
RewriteCond %{REQUEST_URI} ^/account$ [NC]
RewriteRule ^account account_index.php [NC,L]
This means that /account is now linked to account_index.php.
Have a nice day!
Hello i need to make my url clean and i just do not know were to start as it is mind boggling, i have read numerous things in regards to clean urls but i have no idea.
This what i am getting on woorank as i am doing my seo.
Warning! We've detected parameters in a significant number of URLs.
I am unsure if this is right i have taken my real domain out and put my site instead
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^//(\.)?My site/.*$[NC]
#RewriteRule .(png|gif|jpg)$ – [F]
RewriteCond %{HTTP_HOST} !^ My site.co.uk$ [NC]
RewriteRule (.*) My site.co.uk/$1 [L,R=301]
Thank you J C
A basic htaccess can look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^mysite.co.uk [nc]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [r=301,nc]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1 [L]
For every new parameters you just add ([^/.]+)/ and extra parameter in the end..
What happens in code is up to you, but you will need a standard way of working if you want to use something like this.. .
NOTE: you can't just implement this now, because your site needs to be fully build to the structure of your htaccess.. So if you would replace this now, alot of others things might get broken soon... .
It depends. As Naruto pointed out. We need to know the structure of your code. Give some examples of how the urls are now and what you want them to look like. The examples will explain a bit how you might have programmed the website.
e.g. different php file for each page /about.php, /register.php or a single entry /index.php with every page having the same parameter keys.
/index.php?page=page1&foo=bar&qux=norf
/index.php?page=page2&foo=bar
or perhaps each page has different parameter names
/index.php?page=page1&foo=bar
/index.php?page=page2&qux=norf
What you can always do is redirect to a single index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
index.php now needs to route everything approperiately. This basically does the same as what Naruto suggests but instead in php directly and this might be easier for you.
How this routing happens depends on your code and is up to you. But let us assume that you have a different file for each page with different parameters. You could do this without changing the rest of your code.
// FROM: /shop.php?category=software&subcategory=webdevelopment
// TO: /shop/software/webdevelopment
$path = explode('/',$_SERVER['REQUEST_URI']);
if($path[0] == 'shop') {
$_GET['category'] == $path[1];
$_GET['subcategory'] == $path[2];
include('shop.php');
}
This way only one file needs to be edited and the rest of your code can still work with the $_GET. This is the same as what you would do in your htaccess.
Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.
I have an empty .htaccess file
I have a current URL of http://www.website.co.uk/news.php?id=111111
I want this to become http://www.website.co.uk/news/111111
How Do I Do This?
Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111
http://www.website.co.uk/news/city/issue/the-title/111111
How can I make it so that the future changes will work too? So far I have:
RewriteEngine On
RewriteRule ^news/(.+)$ news.php?id=$1 [L]
This still displays the full url and typing in news/111111 redirects to an error page. Please help!
Adding the following to your htaccess should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]
RewriteRule ^news/(.*) news.php?id=$1 [QSA]
The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111
and below will change
RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]
RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111
The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.
In city=London, London will be contained in %1 etc
The second RewriteRule will allow you to find the id used.
I am using codeigniter for my website, and before theres always that index.php? on my every url or links, for example
mysite.com/index.php?/about
Google has indexed all of my urls with that index.php? and I want to remove it and redirect it without that. I am having a problem rewriting the URL and redirect it to mysite.com/about and this what i have tried so far
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [QSA,L,R=301]
what happened is, it only removed the index.php,
for example mysite.com/index.php?/about will turn to mysite.com/?/about I don't know how to remove that question mark,
I'm not good on mod_rewrite thanks in advance for the help.
I think you can improve the rules slightly.
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} index\.php?\?.+
RewriteRule .*$ %{QUERY_STRING}? [R=301,L]
Essentially, you don't have to worry about the entire request line in %{THE_REQUEST}, which removes all the complicated regex. Also, the rule redirects to whatever is listed in %{QUERY_STRING}, and removes the query string.
I am not sure why you used QSA in the first place. I think that was part of the problem earlier. Just for an exercise, you can try removing QSA and see what happens.
You should try this one.
RewriteCond %{QUERY_STRING} ^/[a-z]+$ [NC]
RewriteRule ^/index.php$ %{QUERY_STRING} [NC,L,R=301]
Ok I want to simulate the YouTube URL and redirect it to my real URL. Reason being is I used a random string to make video ID's so they aren't guessable by sequence.
I have the links as so
http://www.mysite.com/watch?v=Dxdotx3iT1
and want to redirect to
http://www.mysite.com/index.php?page=videos§ion=view&v=Dxdotx3iT1
Can't seem to figure out the mod rewrite. Also using a ? I believe tells it to do something.
Any help would be appreciated.
You need to adjust your RewriteRule to include the query string using the [QSA] (query string attached) flag:
RewriteRule ^watch$ index.php?page=video§ion=view [QSA]
RewriteCond %{QUERY_STRING} ^v=(.+)
RewriteRule ^watch /index\.php?page=videos§ion=view&v=%1 [QSA,R=301,L]
None of these answers worked for me, so in the end I found through trial and error the following worked for me;
The RewriteRule to get my URL to look like this https://www.example.com/video/watch?v=UnIq3Id follows;
RewriteRule ^video\/watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/UnIq3Id interfered with my redirecting;
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
The RewriteRule to get my URL to look like this https://www.example.com/watch?v=UnIq3Id is as follows;
RewriteRule ^watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/ interfered with my redirecting;
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Hope it helped someone and saved you the headache I had, people are not so forthcoming on this issue at times How do you write an htaccess RewriteRule to make my urls work like youtube?.