Apache - Changling browser URL address with .htaccess - apache

I tried to change the browser url from localhost:8888/dev/test.php to localhost:8888/dev/testRewrite/ following the example of this question on S.O.
At first, I tried the provided code in that question
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
and managed to change localhost:8888/test.php to localhost/testRewrite/
But applying the same to the sub-directory /dev/ resulted in no change.
I tried adding RewriteBase /dev/, moving the .htaccess to the sub-directory and even clearing the cache but it made no difference and the url remained http://localhost:8888/dev/test.php. So I think I have not understood the rewrite rule properly and am doing it the wrong way.
So what is the right way of doing this? Also does the .htaccess need to be in the sub-directory or it can be in the root directory?

After a little bit more tinkering and searching, I finally got what I needed
RewriteEngine on
RewriteCond %{THE_REQUEST} (\S*?)/test\.php [NC]
RewriteRule ^ %1/test/ [L,R=301]
RewriteRule ^(.*)/test/ /$1/test.php [NC,L]
Regex substitution was the solution to my requirements

Considering your code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
The problem lies in the last line.
^test.php$
In Regex ^ symbolize the start of text and $ the end of text.
Thus ^test.php$ has only one exact match. "test.php" But what you to redirect is "/dev/test.php".
Thus your last line needs to be:
RewriteRule test.php$ /testRewrite/ [L,R=301]
If you want to be very exact, you could also make it
RewriteRule ^/dev/test.php$ /dev/testRewrite/ [L,R=301]
You might need to do the same in line 3.

Related

Htaccess taugh redirection rule

I have to create such a redirection rule in HTACCESS that changes the first part of the URL (from "confindustria/verona/news.nsf/($linkacross)" to "new.bestrank.it/gate/news/documento?openform&id="), keeps unchanged the ID ("879D9288439B4C42C12588E1004BD915&restricttocategory=Credito%20Finanza%20Assicurazioni") but extracts a static piece of it in the middle ("?opendocument").
From
https://old.bestrank.it/confindustria/verona/news.nsf/($linkacross)/879D9288439B4C42C12588E1004BD915?opendocument&restricttocategory=Credito%20Finanza%20Assicurazioni
to:
https://new.bestrank.it/gate/news/documento?openform&id=879D9288439B4C42C12588E1004BD915&restricttocategory=Credito%20Finanza%20Assicurazioni
How should I write the rules for this?
The best I came up with is the following:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/confindustria/verona/news.nsf/($linkacross)/( 879D9288439B4C42C12588E1004BD915)?opendocument(&restricttocategory=Credito%20Finanza%20Assicurazioni)/?\s [NC]
RewriteRule ^ https://new.bestrank.it/gate/news/documento?openform&id=%1%2? [R=301,NC,L]
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Either use following Rules OR use rules of OR here, use them one at a time only please.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/old-path/(id-3434)-newfolder(-5455)/?\s [NC]
RewriteRule ^ https://new-domain.com/new-path/%1%2? [R=301,NC,L]
OR as a Generic rules(not hardcoding digits after id) try following:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/old-path/(id-\d+)-newfolder(-\d+)/?\s [NC]
RewriteRule ^ https://new-domain.com/new-path/%1%2? [R=301,NC,L]

Replace substring in URL via RewriteEngine

im desperately trying to make RewriteEngine to rewrite the following pattern:
https://example.com/api/model/id/
https://example.com/staging/api/model/id/
internally to
https://example.com/index.php/model/id/
https://example.com/staging/index.php/model/id/
I already tried several suggestions from several boards but none of them worked out for me. Ideally the rule should just search for "/api/" and replace it with "/index.php/". I can't figure out, why it is so hard to make that work, my other rules worked out fine till now...
Here is my last try:
RewriteEngine On
RewriteRule ^(.+)/api/(.+)$ $1/index.php/$2 [R=301,L]
# RewriteRule ^/api/(.*)$ /index.php/$1 [R=301,L,NC]
# RewriteRule ^(.+)/api/(.+)$ http://localhost/dev/someFolder/index.php/$2 [R=301,L]
What am I making wrong? I'm just telling the rule to make ($1)/api/($2) to ($1)/index.php/($2), that shouldn't be that hard. Ideally the rule also shouldn't care about whats standing before the "/api/" pattern.
something like this...
RewriteEngine On
RewriteRule ^api/(.*)$ /index.php/$1 [NC]
RewriteRule ^staging/api/(.*)$ /staging/index.php/$1 [NC]
I found a work-around, which seems to work out fine in most use-cases:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) ./index.php/$1 [L]
Found the answer here: htaccess remove index.php from url
Just keep in mind, that you have to put your files in a subdir named after the string you want to replace "index.php" with (e.g. to replace /index.php/$1 with /api/$1 you have to put all your files into a subdir named api).
This is just perfectly fine for my use-case.

Can I use HTACCESS to format this URL?

I would like to know if it is possible to format a URL like https://site1.com/en/article-details.test-article?item=test to https://site1.com/en/article-details/test-article?item=test so basically changing the .test-article to /test-article.
Because of how my site works it only recognizes .test-article as a valid selector so reading up https://httpd.apache.org/docs/2.4/rewrite/flags.html it seems I may need to use a PT flag to show the user the desired URL but pass the correct one to my site.
So far I have this to replace the dot for a slash
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([a-z]{2})(/?)$
RewriteRule ^/([a-z]{2})/.* https://%{SERVER_NAME}/$1/$2 [L,NC,R=301]
Edit(this works to format the URL as per RavinderSingh13's suggestion)
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([^?]*)(/?)$
RewriteRule ^ https://%{SERVER_NAME}/%1/article-details/%2 [QSA,R=301,NE,L]
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.(test-article)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]
As per OP's comments adding more Generic rules here:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.([^?]*)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]
You may try this rule as your topmost rule:
RewriteEngine On
RewriteRule ^(.+)\.(test-article/?)$ /$1/$2 [R=302,NE,L,NC]
Change 302 to 301 after testing this rule.

.htaccess is concatenating rules

I have a .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
this is working for http://www.domain.com/frases/13571/
The point is that I need to redirect all HTTP to HTTPS after this rule is already applied.
So I write this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But isn't working like I supposed to. I'm getting this when I perform a HTTP request:
https://www.domain.com/frases/13571/?categoria=frases&id=13571
What is the right code that just change the protocols without modify the others parts of url?
I think that my code is doing 2 requests in the first rules and that is generating this wrong url.
L flag must be applied to your rewrite rule.
You can use this code instead
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]
Note:
you don't need to escape slashes (\/ can be /)
you'll need to clear your browser's cache before trying again with this code. Your old rule is now in cache and you won't see the new code working if you don't clear it.
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You rewrite from the original REQUEST_URI, the current is what you have captured, so try:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
or
RewriteRule (.*) https://$1 [R=301,L]
If not working, rewrite log could be useful.

Url renaming using .htaccess file

Most are for redirections and removing file extensions or for dynamic urls.
The problem being I can't get a simple static url to rename
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fileThathasalongname file.html
What I currently have 'mysite.co.uk/fileThathasalongname.html'
What I want is 'mysite.co.uk/file/' while file = file.html
using:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^FileIWantToChange.html FriendlyNamed.html
Using this gives me the error multiple Choices
+++++++++++++++++Edit+++++++++++++++++++++++++
Thought i'd add my final version for people to look at, it may help, the anwser below is correct.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{HTTP_HOST} ^www.mysire.co.uk [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]
all works a charm!
I see multiple issues going on. Firstly the regular expression is matched against the friendly URL the user types in. So you need to swap your friendly url and file path with each other. The friendly or "fake" name goes on the left while the url to redirect to silently goes on the right. Also make sure you've set the directory base to /. Finally it's good to add an [L] to enforce it to be the last rule in case anything in the same file tries to rewrite the path. Due note that other htaccess files lower down, depending on the files location, will also be checked even when enforcing the rule has the last rule. Also junk the options part completely. Give this a try:
RewriteBase /
RewriteEngine On
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteRule ^fileThathasalongname.html file.html