.htaccess url parameter rewrite for specific page - apache

I have been battling for hours now and searched Google and StackOverflow, without a solution to my problem, hope you guys can help! :-)
I have a page with parameters as follows: profile?userid=123456789. I want it to rewrite to profile/123456789. This I have, but now everywhere on this page all links gets /profile/link. Is there any fixes to this? Thanks in advance, here comes my .htaccess code.
RewriteEngine on
RewriteCond %{REQUEST_URI} !/profile\.php
RewriteRule ^profile/([^/]*)$ /profile?userid=$1 [L,QSA]

try this, it works for me with many configurations
RewriteEngine On
RewriteRule ^profile/([^/]*)$ /profile?id=$1 [L]
if you want the ([^/]*) to be exclusively number, you'll need to use PHP to rewrite your urls

You can change your rule parameter order as below to make it work
RewriteEngine on
RewriteRule ^profile?userid=([^/]*)$ profile/$1 [L]
Hope this will help you :)

Related

Htaccess rewrite with parameter value

I'm looking for some help. I wanted to redirect the following paramters to another url, how can I make this possible? I have googled alot of other solutions but I fail to make this one work.
https://(www).website.com/reservation#!/confirm?code=a1XapXsCmlaC0
to
https://new-website.com/test/page#!/confirm?code=a1XapXsCmlaC0
Attempted code:
RewriteRule \/reservation#!\/confirm\?code=$ https://test.com/$1
"grep"-ing the code value would make it much easier.
Any help is much appreciated.
Thanks in advance
You may use this redirect rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?website\.com$ [NC]
RewriteRule ^reservation/?$ https://test.com/test/page [L,NC,R=301]
Query string and fragment i.e. part after # will automatically be forwarded to the new URL.
Based on your shown samples, could you please try following. Where you want to redirect from (www).website.com to new-website.com in url.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*) [NC]
RewriteRule ^ https://new-%1/test/page#!%{REQUEST_URI} [QSA,NE,R=301,L]

Redirect urls with query string to seo-friendly directory like urls

I know this question is asked by many users, also I gone through many questions to find the solutions as I am unable to understand the code used in htaccess file. So please kindly help me to solve this issue. I just want to convert a link like:
www.abc.com/project.php?proj-cat=some+project+name
to url like:
www.abc.com/project/some+project+name
OR to like:
www.abc.com/project/some-project-name/
I googled and found a website which creates a htaccess rule for you, using this site I got this:
RewriteEngine on
RewriteRule project/proj-cat/(.*)/ project.php?proj-cat=$1 [L]
but this doesn't redirect to the links above.
Please help me to find the solution for this and help me understand how it works. Thank You!
You can use this code in root .htaccess:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+project\.php\?proj-cat=([^\s&]+) [NC]
RewriteRule ^ project/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^project/([^/.]+)/?$ project.php?proj-cat=$1 [L,QSA,NC]
Reference: 1. Apache mod_rewrite Introduction
2. http://askapache.com

Writing a RewriteRule with just one exception

For a project I need to write a RewriteRule in a .htaccess file but unfortunately I have nearly no experience how to write such rules. What I want to do is rather simple: A complete redirect from one path to another with just one exception. Let me show you my try:
RewriteCond %{REQUEST_URI} !^/success$
RewriteRule ^.*/wop_de/checkout/onepage.*$ http://%{HTTP_HOST}/wop_de/onestepcheckout/ [R=301,L]
What I have thought: If there is no string like "/success" in the URL do a redirect from "/wop_de/checkout/onepage/" to "/wop_de/onestepcheckout/". Well, I guess I was thinking the wrong way as it doesn't work. Could you help me, please?
Also do you know a good tutorial to learn how to write such rules? Thank you in advance!
Use this rule:
RewriteRule ^wop_de/checkout/onepage.*$ /wop_de/onestepcheckout/ [R=301,L]
.htaccess is per directory directive and Apache strips the current directory path (leading slash) from RewriteRule URI pattern.

htaccess URL rewrite for both php and pdf files

I know this comes up often on SO but I'm struggling to understand bits of it and hoping somebody can help.
I need to take the following:
http://www.xyz.com/news.room/showstory.php?storyid=123456
and create
http://www.xyz.com/newsroom/123456
http://www.xyz.com/news.room/
and create
http://www.xyz.com/newsroom/
http://www.xyz.com/files/pdf/uk/Wk51.pdf
and create
http://www.xyz.com/newsroom/convert/Wk51
in addition xyz.co.uk and xyz.org should also be rewritten to xyz.com and all should report back to the search engines as permanent redirects
I'm trying to do this in a htaccess file, that also has a standard CodeIgniter rewrite rule in it to remove the query strings.
Can anyone help me? I've tried several things but can't get anything that works for everything! Also i've spent five hours on this and am slowly pulling my hair out!
Any help, pointing in the right direction etc.. would be appreciated.
Cheers
#redirect any domain other than example.com, to example.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteRule ^newsroom/$ /news.room/
RewriteRule ^newsroom/([0-9]+)$ /news.room/showstory.php?storyid=$1
RewriteRule ^newsroom/convert/([^/]+)$ /files/pdf/uk/$1.pdf

Rewriting a Word in URL

I'm having trouble remove a word from a URL
I want to remove the word 'index' from a URL, I just don't know the code to do it.
I was thinking something like the following:
RewriteRule www.example.com/$ www.example.com/index/$1
Sorry I can't really do much more.
Thanks in advance everyone!
I guess you are using apache.
Use:
RewriteCond %{REQUEST_URI} ^\/index[\/]*$
RewriteRule (.*)$ / [QSA,L]