I've wrote .htaccess file to convert urls to SEO friendly :
the original url is :
http://palestinianz.com/?page=person&p=Alex-Atalla
the content of .htaccess is :
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
it should produce a link like this :
http://palestinianz.com/Alex-Atalla.html
But it makes no effect although I put the file in the root of my website !
where is the problem ?
If you want the URL to change in the address bar, you need to redirect the browser, then rewrite on the server. The first part of this answer explains the process that you want: https://stackoverflow.com/a/11711948/851273
So looking at the second list, the process is to redirect the browser if an ugly link is request to a nice looking link:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page=person&p=([^\ ]+)
RewriteRule ^$ /%1.html? [R=301,L]
Now, on the server end you need to internall rewrite it back to the ugly url, which is more or less what you already have:
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
You can try:
RewriteCond %{REQUEST_URI} (.+)\.html$
RewriteRule ^(.+)\.html$ ./index.php?page=person&p=$1 [L]
Only problem with this is any .html page will go through this. May want to change to the following:
# URL: domain.com/person/Alex-Atalla.html
RewriteCond %{REQUEST_URI} (.+)/(.+)\.html$
RewriteRule ^(.+)/(.+)\.html$ ./index.php?page=$1&p=$2 [L]
This will allow you to have more robustness in changing the the page variable as well
Related
I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]
How could I rewrite my URL using HTACCESS?
When a visitor visits index.php, I want the URL to be rewritten to something else, although the visitor will remain on the index.php page.
This is what I've tried (I did research this before asking but couldn't solve it myself):
RewriteEngine on
RewriteRule ^index.php$ testingit.php
Basically, I just wanted to change index.php to 'testingit.php', just to see if it would work.
You can use this code in your testwebsite/.htaccess file:
RewriteEngine On
RewriteBase /testwebsite/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^ home [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^home/?$ index.php [L,NC]
So here's how you do it:
redirect *** /index.php http://www.yoursite.com/testingit.php
You need to replace *** with one of the following:
301-For a permanent redirect meaning browsers update bookmarks etc.
or
302-For a temporary redirect
Here's a link to a guide I found:
https://www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/
Hope this helps :)
To make pretty URL's:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^home/?$ index.php [NC,L]
My site is: http://binodgiri.com.np, when you click on link you are redirecte to: http://www.binodgiri.com/what.php?id=1
I want to display above url as
http://www.binodgiri.com.np/change-url/
I tried following on .htaccess file
RewriteEngine On
RewriteRule ^change-url/(.*)$ what.php?id=1 [L]
(I don't have much knowledge on this stuff.)
Try this, it should redirect http://www.binodgiri.com/what.php?id=1 to http://www.binodgiri.com.np/change-url/
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /what\.php\?id=1\ HTTP
RewriteRule ^ /change-url\? [R,L]
I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory
I have include a redirect url to redirect www.site.com/machines/ to page
RewriteRule ([^/\.]+)/$ index.php?task=cat&p_name=$1 [L]
as the client wanted to optimize the url to show the machines by name instead of id
But in case of index.php www.site.com/index.php/ it should redirect to index page.
I tired using this condition before the rewrite url
RewriteCond %{REQUEST_URI} !^/index.php/$ [NC]
but this is not completely redirected to page www.site.com/index.php.
Because of this some css and images are not showing properly
So I tried another way
RewriteCond %{REQUEST_URI} ^/index.php/$ [NC]
RewriteRule ^index.php/$ index.php? [R=301,L]
Here it is redirecting like this
http://www.site.com/home/user/public_html/www.site.com/index.php
Please help me with redirect url thanks
You can get rid of the condition here and simply use:
RewriteRule ^index\.php/$ /index.php [L,R=301]
The problem with CSS and images not showing is probably because you're using relative URI's in your html. You need to either change them to absolute URI's (ones with a leading slash) or add a relative URI base to the page header:
<base href="/">
or whatever the base is supposed to be.
I think you forgot to place a slash before the basename:
RewriteRule ^index.php/$ /index.php? [R=301,L]
However, it's looks better if you use this code instead of that:
RewriteCond %{REQUEST_URI} ^/index.php/$ [NC]
RewriteRule ^(.*) /index.php [QSA,R=301,L]