Url rewriting but keeping PHP backend the same? - .htaccess

Let's say I have this URL:
http://mydomain.com/app/editor/?id=59500
I would like to have this url to instead show the following in the browser address bar:
http://mydomain.com/app/editor/59500
...but have the PHP page ("http://mydomain.com/app/editor/index.php") remain the same. In otherwords, I still want to have this page to be able to execute $_GET['id']; and return "59500".
Would I use regex in HTACCESS for this? Any advice on the best approach and an example would be greatly appreciated.

You want to use a .htaccess rewrite for this. You can make the redirect happen only when the URL ends with a number (to avoid redirecting index.php).
The \d+ gets all digits, and the /? will allow URLS that either have a slash after the ID or not.
Something like:
<ifmodule mod_rewrite.c>
RewriteRule ^app/editor/(\d+)(/?)$ app/editor/index.php?id=$1
</ifmodule>

Try adding these rules to the htaccess file in the /app/editor/ directory:
RewriteEngine On
RewriteBase /app/editor/
RewriteCond %{THE_REQUEST} \ /+app/editor/(index\.php)?\?id=([0-9]+)
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index\.php?id=$1 [L]

Related

Make SEO Friendly URL in Php with help of .htaccess

I want to make my URL from this:
https://www.website.com/index.php?title=A1-13-05-2021
to this:
https://www.website.com/A1-13-05-2021
And want to access above query string param in index.php as $_GET['title'].
For this, I have written the below .htaccess code. But that's not working
RewriteEngine On
RewriteRule ^index?$ index.php
RewriteRule ^index/([a-zA-Z0-9\-]+) index.php?title=$1
Can anyone please help me to know where I am doing wrong?
Thanks.
With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for handling non-existing pages here to index.php file.
RewriteRule ^(A1-13-05-2021)/?$ index.php?title=$1 [QSA,NC,L]
Generic Rules: In case you want to make it Generic rules then try following rules only.
RewriteEngine ON
##Rule for handling non-existing pages here to index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(A1-\d{2}-\d{2}-\d{4})/?$ index.php?title=$1 [QSA,NC,L]

htaccess redirection for double slashes

I am having a problem with my site. Sometimes after the user log in is redirected to a page like this:
mydomain.com//somepage
Please notice the double slash in the URL, which takes to a not valid page so i want to modify my htaccess in order to make all pages with double slash to automatically redirect to an URL like this:
mydomain.com/folder/somepage
Please notice the word "folder" between slashes this time. An URL like this would always take to a valid page.
I made some rewrite rules for my htaccess but they dont work as expected:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
</IfModule>
Could you please give me a hint?
Thank you.
Try something like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
You need to match against the %{THE_REQUEST} variable because the URI gets normalized before it gets matched against the pattern of rewrite rules.

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

htaccess rewritecond for url with asterisks

Some... fine person... has set up an incoming link to our site that looks like:
http://www.site.com/*our*-*services*/
I'd like to redirect it so it points it to:
http://www.site.com/our-services/
Ours is a Wordpress site so there's some rewrite stuff in our root htaccess file already. A rule that simply removes asterisks from the URL would do, but I can't figure out how to do that, so I tried the following - loosely based on copying the existing Wordpress rules - which isn't working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/\*our\*-\*services\*/)
RewriteRule . /our-services/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It looks like this rule is being ignored - the errant URL currently redirects you to our default Wordpress 404 page. Clearly, I am fail: what should I have put as the rewrite condition?
I'd remove your first RewriteCond and then just use this rule:
RewriteRule \*our\*-\*services\*\/ /our-services/ [L]
Well get them to change there incoming link.
Asterisks are reserved in urls, so in this case it shouldn't be used:
W3 Url Recommendation
Allowing asterisk in URL

rewriting urls with htaccess

What I need to do is change the links on my website, for example from
www.mywebsite.com/index.php
www.mywebsite.com/index.php?action=about_us
to
www.myswebsite.com/index.html
www.myswebsite.com/about_us.html
I was told htaccess was used to acheive this and I presume this is achieved with some sort of regular expression.
How can I achieve this result?
you have
www.mywebsite.com/index.php?action=about_us
and you want
www.myswebsite.com/about_us.html
in your .htaccess file add a line to the end of this file:
redirect /about_us http://www.myswebsite.com/about_us.html
voila
http://affiliate-minder.com/internetmarketing/using-htaccess-file-for-affiliate-link-redirects/
PK
Turn on the rewrite engine. The RewriteRule will fetch the page in the second parameter when the first parameter is called. So www.myswebsite.com/about_us.html will load www.mywebsite.com/index.php?action=about_us, but the URL in the address bar will still be www.myswebsite.com/about_us.html. The [NC] at the end just makes it case insensitive.
RewriteEngine On
RewriteRule ^/about_us\.html$ index.php?action=about_us [NC]
More info:
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
CheckSpelling on
Options -Indexes
Options +FollowSymlinks
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$\.html /index.php?action=$1 [L,QSA]
and from there, you could use index.php to either navigate, or just display the content

Resources