Replace substring in URL via RewriteEngine - apache

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.

Related

Apache - Changling browser URL address with .htaccess

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.

Understanding RewriteRule & HTACCESS

I'm building a blog with a prety easy page structure, consisting of articles.php, article.php and categories.php and I'd like to tidy up the url path for each page, however I'm having trouble understanding how the mod works.
Currently my articles page is the home page, done using DirectoryIndex, but my url looks like:
http://testblog.local.co.uk/?cat=all&currentpage=3
Where I'd like this to be:
http://testblog.local.co.uk/all/3
My htaccess code so far looks like:
DirectoryIndex articles.php
RewriteEngine On
RewriteRule ^/?([a-zA-Z_]+)/([0-9]+)$ articles.php?cat=$1&currentpage=$2 [L]
This does nothing to the url and does not show the page correctly. I've looked through countless online "Beginner Guides" and still can't work it out. Can anyone help?
Use the %{THE_REQUEST} variable:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /articles\.php\?cat=([^&]+)&currentpage=(\d+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(\d+)/?$ /articles.php?cat=$1&currentpage=$2 [L]
Your rule (last row) needed a slight error correction:
OLD: RewriteRule ^/?([a-zA-Z_]+)/([0-9]+)$ articles.php?cat=$1&currentpage=$2 [L]
NEW: RewriteRule ^([a-zA-Z_]+)/([0-9]+)$ articles.php?cat=$1&currentpage=$2 [L]
Note 1: RewriteRule does NOT require the leading slash "/" and is NEVER met if there is one! In contrast, the RewriteCond require the leading slash...
Note 2: Not sure why you had the "?" char in front of the pattern?! Remaining of your tests?
Rule tested in https://htaccess.madewithlove.com and worked.
The regular expression tested in https://regex101.com .

Rewrite urls friendly with htaccess. how I can use it on my site?

I'm rewriting urls on htaccess using rewrite rules, but when I want that my site uses it I get stuck.
I want my links looks like:
mysite.com/section/this-is-the-title-of-this-section
I used rewrite rules to get this:
mysite.com/section/?id=longalfanum to mysite.com/section/longalfanum
that was cool for a moment. but I checked some few websites for the URLs I realize that they have friendly URL from the beginning
so I changed the url on the to looks like:
mysite.com/section/longalfanum-the-title-of-the-section
now my links doesn't work like before.
my htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#this is for avoid extra /
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule .*/mysite.com/%{REQUEST_URI}? [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#this is my rule
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
obviosly this doesn't work. am I missing a step?
Change
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
to
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
so it matches anything (.*) after 'section/', not just digits (\d+)
Also, you should add an [L] flag to all RewriteRules that don't currently have it to make things a little bit more efficient.
my html with php looks like this:
in the web browser looks like:
mysite.com/section/?idN=alfanum
my rule looks like:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
with this I only get URL like mysite/section/nosensealfanum
it is possible to get the title of new by the htaccess? or I its need to make changes in the way I get the new from the db?
thanks

mod_rewrite for clean URL's not working

I have looked at every question and every example of reWriteRule for an apache server... Nothing has worked.
I just need to change
http://beta.EXAMPLE.com/profile.php?profile_name=ntgCleaner
to
http://beta.EXAMPLE.com/ntgCleaner
I have to see if my .htaccess file is being read and checked to see if mod_rewrite is enabled and they both work perfectly fine.
For some reason, the examples that I have come across are not working for me. Here are some of the examples.
RewriteEngine On
RewriteRule /(.*)$ /profile.php?username=$1
,
RewriteEngine On
RewriteBase /
RewriteRule ^profile\/(.*)$ ./profile.php?profileId=$1 [L,NC,QSA]
and
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?profile_name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?profile_name=$1
But none of these work. Is this because I am using a subdomain? I plan on eventually switching over the subdomain from BETA to just www when I finish the site.
Any advice?
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /profile.php?profile_name=$1 [L,QSA]
Your examples have a lot of different query strings in it, so I'm not sure which one is the one you really want. You have ?username=, ?profileId=, ?profile_name=.
You must use condition (RewriteCond) and when is condition met, rule (RewriteRule) is applicated. So, when you don't use condition, rule is not used, because engine don't know for what to use it.
RewriteCond must be used before RewriteRule, I use it against web-bots in this form:
RewriteCond %{REMOTE_HOST} .googlebot.com$ [NC,OR]
RewriteCond %{REMOTE_HOST} .search.msn.com$ [NC,OR]
RewriteCond %{REMOTE_HOST} .kimsufi.com$ [NC]
RewriteRule ^.*$ /errors/404.html [F]

Help with mod_rewrite rule for dynamic url

Ugh.. mod_rewrite makes me feel stupid. I just haven't wrapped my brain around it yet. :/
I have this url:
http://example.com/a/name/
...that I want to point here:
http://example.com/a/index.php?id=name
...where name is what is getting passed to index.php as the id argument.
Anything I've tried results in either a 404 or a 500.. :(
If you want the trailing slash to be optional, you have to exclude the file you are rewriting the request to. Otherwise you will have a nice infinite recursion.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]
Here any request that starts with /a/… but it not /a/index.php is rewritten to /a/index.php.
But if the trailing slash is mandatory, there is no need to exclude the destination file:
RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]
To start you off:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]
If one rewrite tutorial doesn't work for you, try another.
Edit: excluded index.php as per Gumbo's suggestion
Maybe something along the lines of
RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]
would do the trick.
I suggest you take a look at this URL:
http://www.dracos.co.uk/code/apache-rewrite-problem/
The presented solutions will work, but there are some caveats explained in the URL, mainly regarding ? and # in the URLs themselves.