Mod rewrite and dynamic URLs - php

i know this is a common question but i cannot figure this one out.
I have the following URL:
http://buildsanctuary.com/viewbuild.php?id=3&title=this_is_a_title&page=1
What i wish is the URL to be:
http://buildsanctuary.com/viewbuild/3/this_is_a_title/1
Normally for mod rewrites i would send the user to the link i want and let htaccess do all the work.
So in this case i have tried linking the users to the preferred URL style and rewriting the URL but to no avail.
Any help on how i should be handling this? I want to send the users to the preffered URL but then can i use htaccess to allow me to process the page and URL $_GET information in the same way as the normal dynamic URL?
I have tried the mod rewrite generators etc but nothing works.
This is what the gens have given me:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L]
Thanks.

Fix the generator rule:
RewriteRule ^viewbuild/([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
However, I would do it this way:
RewriteRule ^([^/]*)/(.*) /$1.php/$2 [L]
and then map what you get in $_SERVER['PATH_INFO'] to you preference in PHP.

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&title=$3&page=$4 [L,QSA]

Should do the trick
^viewbuild/([0-9]+)/([a-z_]+)/-([0-9]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [NC,L]

Related

Creating a WordPress-like permalinks rewrite/redirect using PHP and .htaccess, is it possible?

I've been working on a module for a PHP-based application that would allow users to create custom pages. These pages have a URL structure as follows:
yourdomain.com/page.php?url=random-slug
I would like to have these URLs rewritten to use a more SEO-friendly approach:
yourdomain.com/random-slug/
In addition, there should two (2) redirects that accompany this rewrite:
yourdomain.com/page.php?url=random-slug => yourdomain.com/random-slug/
yourdomain.com/random-slug => yourdomain.com/random-slug/
Would this be possible? If so, what would the .htaccess (Apache) file look like to accomplish this? I have tried the following for the rewrite:
RewriteRule ^([^/]*)?.php$ ./page.php?url=$1 [L,NC]
Unfortunately, the above also seems to break other links on the site as well. I'm not exactly sure why yet but was hoping someone could point me in the right direction.
Thank you for your help!
Edit:
I was able to find a way to accomplish this with the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ ./page.php?url=$1 [L]
RewriteCond %{THE_REQUEST} /page\.php\?url=([^\&\ ]+)
RewriteRule ^/?page\.php$ ./%1/? [R=301, L]
Hopefully this will help anyone else with a similar request :)

$_GET attribute from URL without question mark

I'm using plain PHP with no frameworks at all, I want the visitor of my website to access with specific order number, Ex:
http://example.com/index.php?order=123
but I want it to be like this
http://example.com/123
I've found here what I need
$_GET from URL without file extension
but I couldn't understand how to merge it with my current htaccess file, my file currently contains this code which redirect all the http requests to https
this is the code of it
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I hope I've explained my question properly, thanks in advance
you can use PHP URL redirect for value integer
RewriteEngine on
RewriteRule ^([0-9]+) index.php?order=$1 [NC,L]
you can use PHP URL redirect for value alphabet
RewriteEngine on
RewriteRule ^([a-zA-Z]+) index.php?order=$1 [NC,L]
You try rewrite url in htaccess, with your issue:
RewriteRule (.+) index.php?order=$1 [L]
Let view documents in here

how to hide filename using .htaccess , create custom username on url

I want remove file name from url .
my current url is:
http://www.demo.com/user.php?name=joon
and I want to :
http://www.demo.com/joon
if I use this code
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?name=$1
output is
http://www.demo.com/user/joon
but I want
http://www.demo.com/joon
Try this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /user.php?u=$1 [L]
RewriteCond is important here to avoid rewriting your existent directories to /user.php.
This will rewrite /user to /user.php?u=user .
You can achieve this using Apache URL Rewriting. You need to make changes to your .htaccess file. This blog post covers it nicely
https://expozit.wordpress.com/2012/07/25/apache-url-shortening/

how to retrieve a specific get data in htaccess and pass it on rewrite rule

I have a single get data (affid) that i want to retrieve using .htaccess but i don't know how to do it. Can you help me out?
Here is an example of links:
mysite.com/searchmembers?affid=1001
mysite.com/profle/123?affid=1002
mysite.com/videos/567?affid=1003
Another thing that might give a problem is these links already have been rewritten on .htaccess
RewriteRule ^searchmembers? index.php?task=searchMembers
RewriteRule ^profile/(.*)? index.php?task=viewProfile&id=$1
RewriteRule ^videos? index.php?task=videos&id=$1
i just want to retrieve the affid and add it to the links like this:
RewriteRule ^searchmembers... index.php?task=searchMembers&affid=(data retrieved)
RewriteRule ^profile/(.*)... index.php?task=viewProfile&id=$1&affid=(data retrieved)
RewriteRule ^videos? index.php... task=videos&id=$1&affid=(data retrieved)
i know i could add it on htaccess for each of these links but if there is an easier way to do this then it would be a great help. thank you for any response that i will receive!
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^searchmembers$ index.php?task=searchMembers [QSA,NC,L]
RewriteRule ^profile/(.*)$ index.php?task=viewProfile&id=$1 [QSA,NC,L]
RewriteRule ^videos/(.*)$ index.php?task=videos&id=$1 [QSA,NC,L]
If you just need to append a new query parameter (like task here), any existing query parameters can be appended automatically using the [QSA] (Query String Append) flag. I've also corrected the regex used in your RewriteRules. The use of ? is incorrect.

Mod rewrite user URL

I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance
I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.
Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.

Categories