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¤tpage=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¤tpage=$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=([^&]+)¤tpage=(\d+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(\d+)/?$ /articles.php?cat=$1¤tpage=$2 [L]
Your rule (last row) needed a slight error correction:
OLD: RewriteRule ^/?([a-zA-Z_]+)/([0-9]+)$ articles.php?cat=$1¤tpage=$2 [L]
NEW: RewriteRule ^([a-zA-Z_]+)/([0-9]+)$ articles.php?cat=$1¤tpage=$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 .
Related
I searched for this question but only came across very specific answers that I couldn't tailor to my requirements.
My URL now looks like this: https://example.eu/?action=changepassword and I want it to look like this: https://example.eu/changepassword so text ?action= gets deleted.
I tried to adapt this but it didn't work.
With your shown samples, please try following htaccess rules file. Please also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here....
RewriteCond %{THE_REQUEST} \s/?\?action=(\S+)\s [NC]
RewriteRule ^ %1? [R=301,L]
##Internal rewrite rules to handle query string in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?action=$1 [L]
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.
I need help with a redirect using htaccess since I moved my phpbb forum to a subfolder called "forum". So I want viewtopic.php?... to redirect to /forum/viewtopic.php?...
and viewforum.php?... to /forum/viewforum.php?...
I google all day and couldn't find an exact code to use so I tried to fiddle with the code to no avail.
RewriteCond %{REQUEST_URI} !forum/
RewriteRule ^([view(.+)\.php(.+)])$ forum/$1 [QSA,NC,L,R=301]
I tried to catch both "viewtopic" and "viewforum" and then redirect it to /forum/ but it's not working.
This should work for you:
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^view(.+)\.php$ /forum/$0 [QSA,NC,L,R]
Because of the nature of the redirect (with the need to capture the entire request URI), there is no need to wrap it - you can just us $0.
You were using square brackets in your capture, which which would not have helped in any way. Square brackets indicate a character set.
If the new rule works for you, change the R flag to R=301 (as you had it previously), which will make the redirect permanent.
Update: Your entire .htaccess file should look like this now:
Options All -Indexes
RewriteEngine On
RewriteRule ^$ - [E=noabort:1]
# Redirect Forum
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^view(.+)\.php$ /forum/$0 [QSA,NC,L,R]
# WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The redirect rule must come before the WordPress rules.
I've seen variations of this question around, but I couldnt make it work for me.
My problem is: I have many urls indexed at google where the address has one ore more periods. Ex: http://partiturapara.com.br/15-sheets/809-some-music-enc.4.5
Of course the URL does not work, because Apache thinks it is a extension, so I have to strip them, to leave something like this:
http://partiturapara.com.br/15-sheets/809-some-music-enc45
I have a few .htacces lines that make things work (I use joomla) that I will show them bellow.
I´ve tried the following:
RewriteRule ^([^/]*)\.([^/]*)\.$ /$1$2 [L,R=301] - But it just keep loading for ever
and also tried:
RewriteRule ^([^/]*)\.$ /$1 [NC] - This almost works, but gets terrible slow and url will not finish loading.
If you could at least point me in the right direction it would be amazing, cause I already spent lots and lots of hours trying to figure out how all these lines works together and its been very frustrating.
My actual rewrite at the .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# ----- The line I inserted ------
RewriteRule ^([^/]*)\.$ /$1 [NC]
#-------My edit end --------------
RewriteRule .* index.php [L]
EDIT 1
----------------- UPDATE AFTER JON´S SUGGESTION (it didnt work)----------------------
Hi,
After Jon suggestion, I am trying to figure out yet how my htaccess files works. This way I removed all lines in a way the website still works. The Line Jon suggested seems to be ignored. This is minimum .htaccess I got:
RewriteEngine On
------ this is the line Jon suggested me -----
RewriteRule ^(.*)\.([^/]*)\.$ /$1$2 [L,R=301]
------ end line Jon sugested -----------------
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
As I said, the website navigates normally, except when it finds the "dot" in the URL, so it returns the error:
Not Found
The requested URL /15-partituras/1046-sentimental-o.d.m-v-choro was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Perhaps I misunderstood something?
Thanks again!
You don't want to insert that rule there. You've completely usurped the very necessary conditions that need to be applied to the index.php routing rule. RewriteCond's only apply to the immediately following rule.
Try adding this right below RewriteEngine On:
RewriteRule ^(.*)\.([^/]*)\.$ /$1$2 [L,R=301]
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