.htaccess rewrite URL - and playground - .htaccess

I have the following url:
http://example.com/reviews/view-course?courseID=933
Which I'd like to show as:
http://example.com/933/somethingDynamicHere
This is what I'm trying but I get a blank page:
RewriteRule ^([^/]*)$ /reviews/view-course?courseID=$1 [L]
Is there a better way to do this? Also, as importantly, is there a playground where I can try changes and see results on the fly so I can learn about this more?
UPDATE
I also have html5 Angular rewrite rules in the file as well so total is as follows:
#angular html5
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /reviews/index.html [NC,L]

Your pattern is not matching your desired pretty URI. Use this rule:
RewriteRule ^(\d+)/[^/]+/?$ reviews/view-course?courseID=$1 [L,QSA]

Related

.htaccess open file in folder with same name

My project is beginning to get a bit messy and I'd like to clean it up with the .htaccess file.
Right now it's set up to redirect to 'page1.html' or 'page1.php' when I call 'page1'.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
But what I want is when I redirect to 'page1' it opens '/page1/page1.html' or '/page1/page1.php'.
I believe it should be achievable with htaccess but I don't have any experience in it so I'm not completely sure.
Thanks in advance :)
I think you can find your answer in this link (maybe the title doesn't seem to be relevant, but surely it is): https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-centos-7
As the link above mentions, you can use the following to do this (this is used for aliasing a file):
RewriteEngine on
RewriteRule ^page1$ /page1/page1.html [NC]
Hope I've understood what you exactly mean.
Assuming you're trying to do this dynamically, you'd want something like this:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)/?$ $1/$1.php [NC,L]
Problem is that the server has no way of knowing whether it should serve page1.php or page1.html. You'd be better of routing your traffic into a php bootstrapper that does something like check if a file exists and do one or the other if that's your end game.
If you want to maybe just condense your code but still have a bit of bloat to handle which pages go to html vs php, you could do something like this
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(page1|page2|page5)/?$ $1/$1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(page3|page4|page6)/?$ $1/$1.html [NC,L]
That would at least allow you to have just two blocks where you can put each page together.

Rewrite url - Rediriger sans index.php

my URL is of the type: www.mysite.fr and for any page: www.mysite.fr/index.php/test
I wish that www.mysite.fr/test displays www.mysite.fr/index.php/test (it's more beautiful without index.php!)
I tried this but: www.mysite.fr/test displays the home page instead of the test page
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(.*)index\.php/(.*)\sHTTP.*$ [NC]
RewriteRule ^ /%1%2 [R=301,L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*)$ index.php/$1 [L]
Second line to remove index.php exteranlly.
Fifth line to redirect request to original path internally .
Note: clear browser cache the test
Your method works on my localhost but not on my online site. This is the 5th line that blocks in my opinion but I do not see why even adding a slash before index.
Someone have an idea ?

GET parameter doesn't work with htaccess rules

So, I have this code in .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Every file loses its extension (/index.php -> /index etc) and that works perfectly fine. But later on, I started working with admin panel and I'm using some GET parameters there. For example, problematic URL look like:
example.com/admin?cat=1
As far as I know, RewriteRule gets only string after RewriteBase and is not catching GET parameters, right? So why when I try to go to this URL it rewrites it to this?
http://example.com/C:/OpenServer/domains/example.com/admin/1/
There is also this line in .htaccess(but doesn't look like there is problem with it):
RewriteRule ^admin/(.*)$ admin.php?cat=$1
As answered here, you have to add [QSA] at the end of your RewriteRule line:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA]
Google search query that lead me to that answer: htaccess pass parameters

How do I redirect dynamic URLs with parameters?

The old URLs are like this:
http://mywebsite.com/index.php?mode=thread&id=284607
The new URLs would be:
http://mywebsite.com/threads/284607
As you can see, I want to grab the ID # from the old dynamic URLs and point them to the new pretty URLs.
I've tried finding the solution here and elsewhere, but keep having problems removing the "mode=thread" part from the redirect.
Thanks for your help!
Update: Here's some other code that is already in the .htaccess file.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
After a lot of Googling, I found this to work:
RewriteCond %{QUERY_STRING} (^|\?)mode=thread&id=([0-9]+)($|&)
RewriteRule ^index\.php$ http://mywebsite.com/threads/%2/? [R=301,L]
In your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mode=thread
RewriteCond %{QUERY_STRING} &id=([0-9]+)
RewriteRule ^index\.php$ /threads/%1? [L,R=301]

Routing php query using .htaccess

I am trying to find a better solution to solve the issue. I am not using any framework like silex, Yii, etc. Thus I do not have the general routing handling mechanism provided by these frameworks. My URL patterns are like
routing.php
routing.php?action=create
routing.php?action=list&id=1
and the resulting URL I want to have are
/routing
/routing/create
/routing/list/1
I have some very basic understanding of .htaccess, below is my current foundings
RewriteEngine On
RewriteBase /name/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
These codes work well to put any .php to /something. However, I am stuck to continue to generate a general solution for getting /routing/create and /routing/list/1 . I appreciate for any helps.
Have your .htaccess like this:
RewriteEngine On
RewriteBase /name/
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $.php1?action=$2 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?action=$2&id=$3 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Resources