rewriting urls with htaccess - .htaccess

What I need to do is change the links on my website, for example from
www.mywebsite.com/index.php
www.mywebsite.com/index.php?action=about_us
to
www.myswebsite.com/index.html
www.myswebsite.com/about_us.html
I was told htaccess was used to acheive this and I presume this is achieved with some sort of regular expression.
How can I achieve this result?

you have
www.mywebsite.com/index.php?action=about_us
and you want
www.myswebsite.com/about_us.html
in your .htaccess file add a line to the end of this file:
redirect /about_us http://www.myswebsite.com/about_us.html
voila
http://affiliate-minder.com/internetmarketing/using-htaccess-file-for-affiliate-link-redirects/
PK

Turn on the rewrite engine. The RewriteRule will fetch the page in the second parameter when the first parameter is called. So www.myswebsite.com/about_us.html will load www.mywebsite.com/index.php?action=about_us, but the URL in the address bar will still be www.myswebsite.com/about_us.html. The [NC] at the end just makes it case insensitive.
RewriteEngine On
RewriteRule ^/about_us\.html$ index.php?action=about_us [NC]
More info:
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

CheckSpelling on
Options -Indexes
Options +FollowSymlinks
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$\.html /index.php?action=$1 [L,QSA]
and from there, you could use index.php to either navigate, or just display the content

Related

Url rewriting but keeping PHP backend the same?

Let's say I have this URL:
http://mydomain.com/app/editor/?id=59500
I would like to have this url to instead show the following in the browser address bar:
http://mydomain.com/app/editor/59500
...but have the PHP page ("http://mydomain.com/app/editor/index.php") remain the same. In otherwords, I still want to have this page to be able to execute $_GET['id']; and return "59500".
Would I use regex in HTACCESS for this? Any advice on the best approach and an example would be greatly appreciated.
You want to use a .htaccess rewrite for this. You can make the redirect happen only when the URL ends with a number (to avoid redirecting index.php).
The \d+ gets all digits, and the /? will allow URLS that either have a slash after the ID or not.
Something like:
<ifmodule mod_rewrite.c>
RewriteRule ^app/editor/(\d+)(/?)$ app/editor/index.php?id=$1
</ifmodule>
Try adding these rules to the htaccess file in the /app/editor/ directory:
RewriteEngine On
RewriteBase /app/editor/
RewriteCond %{THE_REQUEST} \ /+app/editor/(index\.php)?\?id=([0-9]+)
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index\.php?id=$1 [L]

Rewrite rule not working as expected?

I have a URL with a parameter which I wish to make into sef URL:
want:
http://map.tautktiv.com/street.php?address=abc
to become:
http://map.tautktiv.com/street/address/abc
or
http://map.tautktiv.com/address/abc
have tried several online tools to generate a .htaccess rule, but none of them have any effect on the URL, .htaccess file is active (tried to put some gibberish in it and got error 500)
these are the rules I tried:
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1
2.
Options +FollowSymLinks
RewriteEngine on
RewriteRule /address/(.*)\.php street.php?address=$1
3.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here
RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1
the site is a sub-domain which files reside in a sub directory in a shared hosting GoDaddy server, have also tried to apply these rules to the .htaccess in the directory above it, same result.
tried also this per below suggestions
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
same result, nothing happens.
tried to go directly to page from main domain but same result:
http://tautktiv.com/map/streets/street.php?address=abc
First rule will redirect your ugly URL to the pretty URL.
Second rule will internally redirect it back so the user will not see the ugly URL.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]
# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]
If you confirm the rule to be working as expected then you can change it from 302 to 301 as you do not want to use 301 until you know the rule is working as expected.
The .htaccess should go inside the folder where street.php is located.
HTTP is US ASCII so your language would fail, it will redirect it to something like this:
/street/address/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7%259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C
Your best bet here would be to change the links to use /street/address/word instead of the php file directly.
This way you would not need the first rule and you can use only the internal redirect which would work just fine with this update.
Try this one:
RewriteEngine on
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
In your examples you'd missed ^ and $ in the second row of RewriteRule.
And use [r=301,L] instead of [L] to tell the browser, that thzis is premanent redirecting.

using - instead of _ (underscore) with .htaccess

I'm using with good results the following code to access alla of my php files into the /it directory without specifying the extension. In other words I can access to "http://www.mydomain.com/it/about.php" just writing "http://www.mydomain.com/it/about".
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/it/$1.php [L]
the same happen when i try to access to http://www.mydomain.com/it/question_answers.php.
How can I access directly to *"http://www.mydomain.com/it/question_answers.php"* also writing "http://www.mydomain.com/it/question-answers"?
I wrote the floowing code below the previous but it seems not to work.
Redirect 301 /question-answer http://www.mydomain.com/it/question_answer.php
because if i write "http://www.mydomain.com/it/question-answer" the browser try to open the page:
"http://www.mydomain.com/it/question-answer.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php"
A small abstract of the post:
I have the page *"http://www.mydomain.com/it/question_answers.php"*
with the first part of code I can get it using the link *"http://www.mydomain.com/it/question_answers"*
I'd like to access the same page also with the following "http://www.mydomain.com/it/question-answers"
Thanks!
This should work for you:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/$1.php
RewriteRule it/question-answer\.php http://www.mydomain.com/it/question_answer.php [R=301,L]
I have changed two things: I deleted it/ in the new URL of the first RewriteRule. Otherwise you would be redirected to it/it/
I also added \.php to the second RewriteRule. I don't really know why, but the RewriteRule seems to replace the pattern instead of redirecting. And if your pattern is it/question-answer and the real url is it/questions-answer.php the .php will not be replaced.

How to redirect in .htaccess? => /ua/ to /?lang=ua

I have a multilingual website and .htaccess, which displays the all page and language ?lang=ua style.
I want to redirect (using code 301) asks site.com/en/ to site.com/?lang=en with RewriteEngine.
Example:
site.com/en/ => site.com/?lang=en
site.com/ua/news.html => site.com/news.html?lang=ua
site.com/ua/news/2-material-two.html => site.com/news/2-material-two.html?lang=ua
and so on much...
How to prepare Htaccess file for Apache to meet this criterion? And how do?
Thanks in advance
Put this code in your .htaccess file under DOCUMENT_ROOT dir:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)/(.*)$ /$2?lang=$1 [L,R=301,QSA]
Try this:
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteBase /
RewriteRule http://site.com/([^/]+)/(.+).html$ http://site.com/$2.html?lang=$1 [R=301,NC]
I've just put this together off the top of my head so it may not be quite right, but the idea is that it will examine the url and the ([^/]+) should read the 1st directory, and then read everything up to the .html.
It then rewrites the url to be site.com/[everything after 1st dir up to .html].html?lang=[name of 1st dir]
Add the following to your htaccess file in the root of your domain
RewriteEngine On
RewriteBase /
#if the lang param is not present
RewriteCond %{QUERY_STRING} !^lang=
#capture the language code and redirec to url with lang param
RewriteRule ^([a-zA-Z]+)/(.+\.html)$ /$2?lang=$1 [L,R=301,NC]
Please, if you must use general "possibly directory matching"-rule, consider the !-d operator (and optional: the !-f operator).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/ http://site.com/?lang=$1 [R=301,L]
So you won't end up fixing all kinds of strange problems with existing files and/or directories.

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources