.htaccess redirection issues with rewrite rule - .htaccess

I am running a website on apache and the problem i am facing is below .
The actual link looks like this :
www.domain.com/my-account/profile-view.php?id=1018428
where id is the user id of the user .
Now my .htaccess looks like this :
RewriteRule ^my-account/([0-9]+)/([a-zA-Z0-9-]+)$ my-account/profile-view.php?id=$1
Basically i want the URL to be rewritten like this :
www.domain.com/my-account/15/Craig
I've done different permutations to the above htaccess line and none seem to work . Any help would be appreciated .
P.S : My htaccess is working fine and is being read by the server .

maybe this will help you
RewriteRule ^my-account/([0-9]+)/([a-zA-Z0-9-]+)/$ my-account/profile-view.php?id=$1&name=$2 [nc,l]
You need two variables since you have two regex in your url:
- ([0-9]+) for the id
- ([a-zA-Z0-9-]+) for the username
The nc flag means non-case-sensitive (so Craig will also match craig)
the l flag means "last", when rule is validated, stop processing the htaccess rules below that point

Related

how to resolve htaccess redirect issue with website

My website is not stripping the text in * condition.
Website URL is like http://www. domain.com/search/adasd
I want to redirect it to https://www. domain.com/noindex-page
Using below redirect rule
RewriteRule ^search/\*$ https://www. domain.com/noindex-page? [L,R=301]
but it is redirecting to https://www. domain.com/noindex-pageadasd and giving 404 not found.
Please suggest how to strip adasd from this.
Using * is necessary as there are number of URLs with this prefix.
Based on your shown samples could you please try following. I believe problem in your regex \* is you have escaped * which means it is NO longer matching everything after search keyword and is treated as a literal as * hence it may not be working. Since you are looking for anything after search keyword then we could simply check if uri starts from search here.
RewriteEngine ON
RewriteRule ^search https://www.yourdomain.com/noindex-page? [L,R=301,NC]

Rewrite/Redirect a specific URL with masking

I need to add Rewrite/Redirect statements in my .htaccess to redirect a specific url to a different .php page with masking. The goal is when someone hits:
www.mydomain.com/sandbox
i need it to go to:
server.mydomain.com/directoryname/phpprogram.php. But I need it to mask so the address bar will still show www.mydomain.com/sandbox
Appreciate any help with this. Thanks.
You can use mod-rewrite to maks your URLs.
The following rule in an htaccess file should do the trick :
RewriteEngine On
RewriteRule ^sandbox/?$ /directoryname/phpprogram.php [L]
This will internally map a request for example.com/sandbox to example.com/directoryname/phpprogram.php .
The /? in the regular expression pattern above matches the traling slash as an optional character so the rule will also match example.com/sandbox/ .

How to rewrite a url with id and title passed

Basically, what i want is this:
For example, I have a url that looks like this :
example.com/stories/show.php?id=$id
I want that url to look like this
example.com/stories/$id/$title
And I want the title to look like this:
I love stackoverflow to
I-love-stackoverflow
Seperating each words with a dash.
Am I supposed to pass the title along with the id??
e.g example.com/stories/show.php?id=$id&title=$title
And how do I achieve all this with .htaccess ??
A good example of what I want to do is used by stackoverflow.. Check the url of this question.
You only need some simple rule like in the below example
RewriteEngine on
# rewrite stories/<id>/<title> to show.php
RewriteCond %{REQUEST_URI} stories/([0-9]+)/([a-z\-A-Z]+)
RewriteRule (.*) show.php?id=%1&title=%2 [L]
Place the above in your .htaccess file, which should be located under the stories folder. Of course you also need to make sure so that the rewrite module is enabled on your server. This could be done by running the following commands from the terminal:
sudo a2enmod rewrite
sudo apachectl restart
Now you should be able to rewrite an url like:
http://example.com/stories/666/stack-overflow
to:
http://example.com/stories/show.php?id=666&title=stack-overflow
Then you could create your links in the following manner:
<?php
// php example
$title = "I love stackoverflow";
$id = 666;
?>
link
I think you should read up on how url rewriting really works, here is a couple of links that might be useful:
URL Rewriting for Beginners
URL Rewriting Guide

htaccess rules to redirect in the same directory

I am new in htaccess rules. I have tried to search about my question but couldn't get the answer.
I have got a url like this mysite.it/test/libro/la-missione-cristiana/1959 and I would like to rewrite to mysite.it/test/index.php?page=libro&id=1959
I have the following htaccess file in my root folder and I have added the following line:
RewriteRule ^test/libro/([^/.]+)/([^/.]+) test/index.php?page=libro&id=$2 [QSA,L]
but It doesn't work.
I would really appreciate if someone could explain me the cause.
Thank you,
your match all but slash group '([^/.]+)' appears to group all non-slash and non-any-character, thus it will not match anything as the . matches any, and the plus require at least one match.
try remove the dots
RewriteRule ^test/libro/([^/]+)/([^/]+) test/index.php?page=libro&id=$2 [QSA,L]
should work for you.

htaccess rewrite, url doesn't work when i add a / at the end

I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.
i've got the following line in my htaccess (root map)
RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1
When i enter the the following url it works without a problem
www.myexample.com/page1/variable
the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.
Thanks for your time and help!
Get rid of the ending /$ sign on the first rule
RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
Or you can continue to capture data
RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing
RewriteRule ^page1/(.*)$ page1.php?url=$1
Then use the server side script to determine what to do.

Resources