How to change url stucture with htaccess - php

I have this url myurl.com/index.php?hello=folder/7f6c06
I want to change it to myurl.com/folder/7f6c06
I tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?hello=$1 [QSA,L]
is not working in godaddy hosting.

It doesn't work because your pattern ([^/]+) captures everything up to but not including the first /, but the URI you are looking to capture and pass into hello= includes a / itself. Since there are additional characters after the first / but the pattern matches at most one / optionally at the end, the pattern would never match as you have it.
Instead just use (.+)/? to capture everything up to an optional possible trailing slash.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?hello=$1 [QSA,L]

Related

Passing Different Parameter for Same File using URL Rewrite

I have user.php file and I m passing two parameters with it using URL Rewrite
but I m unable to use this rule as whichever rule is written first only gets executed and not the second one in case of my rule for userID rule doesn't work but work for webName. Any solution will be very much helpful to me.Thank you
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(.*)$ user.php?webName=$1 [L,QSA]
RewriteRule ^user/(.*)$ user.php?userID=$1 [L,QSA]
Make a difference between two regural expression:
If you would like to catch user ID you should filter for only numbers: \d
Then if there is another chars in url not only numbers you can match for anything except kslash: [^/]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\d+)$ user.php?userID=$1 [L,QSA]
RewriteRule ^user/([^/]+)$ user.php?webName=$1 [L,QSA]

mod_rewrite issue (multiple slashes address?)

I'm no regex expert and I'm trying to implement on a website a social login system that may or may not redirect the user to the page where he was before loggin in.
Like so, for no redirection:
RewriteRule ^login/([^/]+)$ login.php?p=$1 [L]
http://example.com/login/Facebook >> the page does it stuff and goes to index (default behaviour).
or like this for redirection (p = provider; r= redirect relative path):
RewriteRule ^login/([^/]+)/([^/]+)$ login.php?p=$1&r=$2 [L]
Works great if the user is on a page like http://example.com/info that is generated by the following rule:
RewriteRule ^info$ users_infosystem.php [L]
I'm having problem when the user is on a page like http://example.com/gallery/galleryname
RewriteRule ^gallery/([^/]+)$ galery.php?name=$1 [L]
Any Help?
EDIT: Just occured me ... I don't know if the $ char is the delimeter that marks the end and taking it off makes the rule accept everythings that is after the ([^/]+) bit.
Also: Doesn't the ([^/]+) bit match everything but a forward slash?
I'm having problem when the user is on a page like
That is a very vague sentence. You don't actually say what the problem is. What about your rule is not working? Also didn't paste your entire htaccess file, you pasted bits and pieces so we don't even know what order your rules are in or if there are other rules.
I don't see an issue with these rules. These rules should work. They all match something different. Also you can make the / optional using the ? after it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/([^/]+)/?$ login.php?p=$1&r=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/?$ login.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^info/?$ users_infosystem.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/([^/]+)/?$ galery.php?name=$1 [L]
The last rule will match http://example.com/gallery/galleryname with or without a / at the end.

htaccess with url rewriting not working

This is my htaccess code:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)$ http://www.example.com/index.php?id=82&user=$1 [L,R=301]
Obviously I want this URL:
www.example.com/username
to be translated to http://www.example.com/index.php?id=82&user=username
This does not work however.. (this code results in the htaccess not working at all and getting a Page not found error.
If I change the ]+$ for a ]+? the code does work, but not like I want:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)? http://www.example.com/index.php?id=82&user=$1 [L,R=301]
Results in the URL being rewritten/redirected to http://www.example.com/index.php?id=82&user=index ... Exactly like that, so with user=index.
Now, if I remove the RewriteRule .* /index.php line, the htaccess again doesn't work at all anymore, resulting in a Page not found error...
I've spent days and days on figuring this out but I'm absolutely clueless..
So, I just want www.example.com/username to redirect to http://www.example.com/index.php?id=82&user=username
There are several issues here.
Inside a .htaccess file, patterns are matched "against the filesystem path, after removing the prefix". This means, you will have no leading slash as in /typo3 or ^/([a-zA-Z0-9_-]+)?
The pattern ([a-zA-Z0-9_-]+)? matches also an empty request, because of the trailing ?. I guess, this is not what you intended.
Rules are processed in sequence, unless you do a redirect [R] or add an [L] flag. This is the reason why first the request is rewritten to index.php and then in the next rule index is recognized as the user and again rewritten to .../index.php?id=82&user=index
Which leads to the next problem between the patterns .* and ([a-zA-Z0-9_-]+). .* recognizes all requests, including every user. So there is no way to distinguish between a user and any other request.
To rewrite usernames, you could try
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+) http://www.example.com/index.php?id=82&user=$1 [L]
This means, if the request doesn't correspond to an existing file !-f or directory !-d, then presume it's a username and rewrite to index.php?....
If you don't want a redirect, leave out the host name
RewriteRule ^([a-zA-Z0-9_-]+) /index.php?id=82&user=$1 [L]

Modifying .htaccess rewrite rule for MVC application

This is the rewrite rule I currently use for my MVC application:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/([^/]+))?$ application/controller/$1.php?method=$2&param=$4
Displays as: www.website.com/controller/method/param
The param URI is optional. How would I make method optional as well, so it will allow the user to go to www.website.com/controller?
Could I also additionally force the url to have a trailing slash?
Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+)|)(?:/([^/]+)|) application/controller/$1.php?method=$2&param=$3 [L]
The (?:/([^/]+)|) are optional capture groups that allows for a "nothing" using the | symbol.

PHP regex for .htaccess

Say I have a page testpage.php and I want to access it through localhost/testpage . I can't figure out the regex to get it to work.
When I use the following, I get a server error
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* "^[^0-9][A-z0-9_]+*.php"/$0
However, when I specify the exact file it works.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* testpage.php/$0
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
the third line checks if the file actually exists before doing the rewrite to avoid errors
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php$
RewriteRule ^(.*)$ $1.php [L]
It will rewrite the filename with .php extention
You cannot specify a regular expression in the replacement pattern. That's why it fails.
In this line
RewriteRule .* "^[^0-9][A-z0-9_]+*.php"/$0
You are telling Apache to rewrite anything ( .* ) to the literal "^[^0-9][A-z0-9_]+*.php"/, plus the zeroth matched string (which is an error anyway, because matches start from $1, then there is $2 etc.).
If you want to capture a match you must surround it with parentheses, like
(.*)
and then it will be available in the replacement string as $1.

Categories