How to create permalink in htaccess - .htaccess

I want to redirect a link to another with .htaccess file in Linux host. Can you help me?
from: http://example.com/examp
to: http://example.com/examp.php
And another one for my other site
from: http://example.com/examp
to: http://example.com/user.php?u=examp

You will need mod_rewrite enabled for this. Start with placing these lines into .htaccess:
RewriteEngine On
RewriteBase /
TBH I'm not 100% sure what do you mean exactly by permalink and how do you want to redirect, so I will provide 2 variants for each URL: rewrite (internal redirect) and redirect (301 Permanent Redirect).
1. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/examp.php while URL will remain unchanged in browser:
RewriteRule ^examp$ examp.php [L]
2. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/examp.php [R=301,L]
3. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/user.php?u=examp while URL will remain unchanged in browser:
RewriteRule ^examp$ user.php?u=examp [QSA,L]
4. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/user.php?u=examp [QSA,R=301,L]
Useful link: http://httpd.apache.org/docs/current/rewrite/

You will need mod_rewrite enabled for this
from: http://example.com/123
to: http://example.com/index.php?q=123
RewriteEngine on
RewriteBase /
RewriteRule ^/?([-A-Za-z0-9]+)/?$ index.php?q=$1 [QSA,L]

You'll want to look at RewriteRules and know/understand regular expressions. It'll be something like this:
RewriteEngine on
RewriteRule ^(.*)\/examp$ /examp.php [R=301,L]
- and -
RewriteRule ^(.*)\/[a-zA-Z0-9\-\_\.]+$ /user.php?u=$1 [R=301,L]
The latter example will take what's in-between the [] and place it in the $1 variable
Here is a good link to get you started:
http://www.webweaver.nu/html-tips/web-redirection.shtml

Related

Problem with using Rewriterule in htaccess

With RewriteRule I've always cleaned my URLs as following:
RewriteRule ^page/(.*)$ page.php?urlkey=$1 [QSA]
My new host doesn't allow me to use Options +FollowSymLinks and therefore I cannot use the / anymore. So I've changed my RewriteRule to:
RewriteRule ^page-(.*)$ page.php?urlkey=$1 [QSA]
However, I need to redirect all my former URLs to the new version. I tried doing this using the following rule:
RewriteRule ^page/(.*)$ page-(.*)$ [R=301,L]
This is however not working. I've also tried to just make a Redirect in my .htaccess:
Redirect 301 https://www.example.com/page/urlkey https://www.example.com/page-urlkey
This is also not working.
EDIT
As requested the actual code below:
RewriteEngine on
RewriteRule ^citywalk-(.*)$ citywalk.php?urlkey=$1 [QSA]
RewriteRule ^citywalk/(.*)$ citywalk-(.*)$ [R=301,L]
For example the citywalk The Historical Centre has a urlkey the-historical-centre. The old url is citywalk/the-historical-centre.
To test this specific case and other technique:
Redirect 301 /citywalk/the-historical-centre https://example.com/citywalk-the-historical-centre
By visiting https://example.com/citywalk/the-historical-centre no redirecting takes place (the url stays the same in the browser) and no urlkey is found.

Redirect specific URL to another URL on another subdomain

I have 5 URLs on a subdomain website http://subdomain.example.com, and I want them to redirect to 5 other URLs on my main website, https://www.example.com/.
Important: URLs do NOT have the same structure!
Example:
http://subdomain.example.com/url1 should redirect to https://www.example.com/ipsum
http://subdomain.example.com/url2 should redirect to https://www.example.com/lorem
etc.
How can I handle that?
UPDATE:
There is a play folder (name of the subdomain) which contains the subdomain website files and a htdocs folder which contains the www website files.
Here is the .htaccess file in my play folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Since the subdomain has it's own .htaccess file, you don't need to specify the hostname as part of the redirect. And since you already have mod_rewrite directives in the subdomain's .htaccess file, you should also use mod_rewrite for these redirects (to avoid conflicts). Otherwise, you'll need to specify these redirects one-by-one.
Try the following at the top of your subdomain's /play/.htaccess file. Note that this needs to go before the existing directives in the file.
# Specific redirects
RewriteRule ^url1$ https://www.example.com/ipsum [R=302,L]
RewriteRule ^url2$ https://www.example.com/lorem [R=302,L]
The above would match a request for http://subdomain.example.com/url1 and redirect accordingly, etc.
Note that the RewriteRule pattern (regular expression) does not start with a slash when used in a per-directory (.htaccess) context.
Note that these are 302 (temporary) redirects. Change them to 301 (permanent) - if that is the intention - only once you have confirmed they are working OK (to avoid caching issues).
Try this...
Redirect 301 http://subdomain.example.com/url1 https://www.example.com/ipsum
Redirect 301 http://subdomain.example.com/url2 https://www.example.com/lorem

301 redirect: redirect urls with .php to folder

In .htaccess I want to redirect urls like this:
/products.php/a7-frames
/products.php/a6-frames
to this:
/picture-frames/a7-frames
/picture-frames/a6-frames
So need to substitute products.php with picture-frames.
After a lot of Googling I tried this:
RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1
But it doesnt work, if I enter this url: /products.php/a7-frames the browser says there are too many redirects and goes to:
/picture-frames/index
It's substituting the products.php for picture-frames which is great, but I'm not sure why it adds "index" on the end rather than the /a7-frames part of the url? How can I fix this?
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]
Make sure to clear your browser cache before testing this change.

Unable to get .htaccess RewriteRule to work

I've been pulling my hair out trying to get a URL rewrite rule to work using .htaccess. Mod_rewrite is enabled and I have managed to get a 301 redirect to work (from /beta to /Beta/) so I know the .htaccess is able to work.
Basically I'm trying to get /Beta/Page.php?id=page&tab=services&tabid=tab1 to become /page/services (and ideally leave out the tabid if it's not going to break the site removing it).
The code I'm working with currently is:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3
redirect 301 /beta http://www.example.com/Beta/
Any help would be gratefully received.
Remove the leading slash:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(Beta)/[^.]+\.php\?id=([^&]+)&tab=([^\s&]*)&tabid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=302,L]
RewriteRule ^Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3 [L,QSA]
This will externally redirect:
/Beta/Page.php?id=page&tab=services&tabid=tab1
to
/Beta/page/services/tab1
and rewrite same URI internally.
Also .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

URL Rewriting in. Htaccess

I have a website on a test server and I want to rewrite URL for this website because it is very long
I wish our visitors instead of entering this URL:
http://staging.company.fr/site2.it/s...oject2/public/
enter this URL:
www.monsite.com
I created a file. htaccess:
RewriteEngine On
RewriteRule ^/~(.+) http://www.monsite.com/~$1 [NC,L]
but does not work
While in .htaccess mod_rewrite doesn't match leading slash in a URL since it is applied per directory. Therefore following should work:
RewriteEngine On
RewriteRule ^(.+)$ http://www.monsite.com/$1 [L,R=301,NE]
This will redirect every URL in your existing domain other than home / to monsite.com
Reference: Apache mod_rewrite Introduction

Resources