1

i want to change url by htaccess from http://localhost/projectname/cab-details.php?v_name=variable to http://localhost/projectname/variable Here variable is a link(slug) Now My htaccess code is

Options +FollowSymlinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php

RewriteRule ^$1/([a-zA-Z-0-9-]+) cab-details.php?v_name=$1
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • `RewriteRule ^$1/([a-zA-Z-0-9-]+)` makes little sense; `$1` is a back reference to the first capture group of the _current_ pattern. – CBroe Oct 17 '22 at 13:30
  • that is possible or not sir.. – Srikanta Sethy Oct 17 '22 at 13:35
  • The condition `RewriteCond %{REQUEST_FILENAME}\.php -f` for the URL `/test` is literally "if a file called test.php **does not** exist", and it doesn't make sense to route the request to such a file then – Chris Haas Oct 17 '22 at 14:44
  • Also, if you are more comfortable in PHP than htaccess, maybe consider using a single router script with [FallbackResource](https://stackoverflow.com/a/16389034/231316) – Chris Haas Oct 17 '22 at 14:47

1 Answers1

0

With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteBase /omm/
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/(omm/[^.]*)\.php\?v_name=(\S+)\s [NC]
RewriteRule ^  /%1/%2? [R=301,L]

##Internal redirect rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^omm/([^/]*)/([^/]*)/?$  $1.php?v_name=$2 [QSA,NC,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • my php page click link code is `View Details` Please help me here changes.. – Srikanta Sethy Oct 18 '22 at 12:31
  • @SrikantaSethy, Could you please do let me know what sample link you have hit and what's happening once you hit it? Thank you – RavinderSingh13 Oct 18 '22 at 12:33
  • After sample link click change page and here link `http://localhost/omm/cab-details` here not get variable also error occurs `Warning: Undefined array key "v_name" in D:\xamp\htdocs\omm\cab-details.php on line 5`. Please help me in link changes – Srikanta Sethy Oct 18 '22 at 12:56
  • @SrikantaSethy, Sorry to say but this is NOT clear. Please tell these things: **1st:** What link you are hitting? **2nd:** What is happening after hitting that link? **3rd:** what is happening to link after using my htaccess code. Let me know on these details in detailed so that I can help you, cheers. – RavinderSingh13 Oct 18 '22 at 12:57
  • **1st** hitting link ` – Srikanta Sethy Oct 18 '22 at 13:13
  • @SrikantaSethy, Ok I hope you have put my htaccess rules in your file. Now please do let me know when you hit `http://localhost/omm/ac-swift-dzire` link what happens in browser OR in backend? NOT from code side from htaccess side please. – RavinderSingh13 Oct 18 '22 at 13:18
  • I have changed the htaccess code only project name **omm**. Like `RewriteCond %{THE_REQUEST} \s/(omm/[^.]*)\.php\?v_name=(\S+)\s [NC]` and `RewriteRule ^(omm)/([^/]*)/?$ $1/cab-details.php?v_name=$2 [QSA,NC,L]` . After done this hitting link then show in browser **Not Found** or **The requested URL was not found on this server.** . In Browser console **Failed to load resource: the server responded with a status of 404 (Not Found)** After hitting link then link is **http://localhost/omm/ac-swift-dzire** Actually i want to this link. – Srikanta Sethy Oct 18 '22 at 13:30
  • @SrikantaSethy, Ok now you have given some information which can lead us to FIX. So you are getting 404 error. 2 questions here: **1st:** where is your .htaccess located? Mention full path here. **2nd:** where is your php file(s) present mention their fill path also here. Let me know cheers. – RavinderSingh13 Oct 18 '22 at 13:32
  • **1st** me .htaccess file in root folder (omm - this my project folder - http://localhost/omm/). **2nd** hitting link php file (index.php) also root folder (omm - this my project folder - http://localhost/omm/). **3rd** redirect to php (cab-details.php) also o root folder (omm - this my project folder - http://localhost/omm/). – Srikanta Sethy Oct 18 '22 at 13:41
  • @SrikantaSethy, Ok could you please change last line `RewriteRule ^(projectname)/([^/]*)/?$ $1/cab-details.php?v_name=$2 [QSA,NC,L]` **To** `RewriteRule ^(projectname)/([^/]*)/?$ cab-details.php?v_name=$2 [QSA,NC,L]` once and save your .htaccess file and clear your browser cache and try again. Let me know how it goes. – RavinderSingh13 Oct 18 '22 at 13:44
  • After `RewriteRule ^(projectname)/([^/]*)/?$ cab-details.php?v_name=$2 [QSA,NC,L]` replace also change project name like `RewriteRule ^(omm)/([^/]*)/?$ cab-details.php?v_name=$2 [QSA,NC,L]` then save and clear cache in browser. Same problem show in browser. Not Found or The requested URL was not found on this server. . In Browser console Failed to load resource: the server responded with a status of 404 (Not Found) . – Srikanta Sethy Oct 18 '22 at 13:53
  • @SrikantaSethy, I think we both are not in sync. Let me clarify what I know about your question here. 1st: You are hitting link `http://localhost:80/projectname/test_singh.php` which should redirect to `http://localhost:80/test_singh` **then** it should internally rewrite(or being served by) file cab-details.php? This is what my understanding as of now, please do confirm me if I am right here? Thank you. – RavinderSingh13 Oct 18 '22 at 13:57
  • I want to change my link after hitting a link. In PHP `View Details`. This My hitting link with variable (v_link). This hitting link PHP file (index.php) And link redirect PHP file (cab-details.php) Same Folder also .htaccess file same folder in my project. All Three files are root folder of my project (omm). omm is my project folder. After click link redirect to cab-details.php with variable like (http://localhost/omm/cab-details.php?v_link=ac-swift-dzire) here (ac-swift-dzire is v_link - variable). This is PHP is good working – Srikanta Sethy Oct 18 '22 at 14:25
  • But I want to change Here link after clicking link like (http://localhost/omm/variable-)**http://localhost/omm/ac-swift-dzire/** Here **omm** is project name ** ac-swift-dzire** valiable(v_link). please help me. – Srikanta Sethy Oct 18 '22 at 14:25
  • @SrikantaSethy, I am sorry its not clear, could you please mention url in code tags Eg: `http://localhost:80/test` use backticks to get comment values in code tags. – RavinderSingh13 Oct 18 '22 at 14:50
  • After hitting the link redirect to another page then link is `http://localhost/omm/cab-details.php?v_name=ac-swift-dzire` in PHP. I want to change this link `http://localhost/omm/ac-swift-dzire` . Please help me. – Srikanta Sethy Oct 18 '22 at 15:06
  • @SrikantaSethy, I have updated my rules now, please do check once and let me know if this helps you? – RavinderSingh13 Oct 18 '22 at 15:53
  • After Update .htaccess code and clear browser cache. Then run-in browser i got url `http://localhost/omm/cab-details/ac-swift-dzire` and `The requested URL was not found on this server.` . In browser console `Failed to load resource: the server responded with a status of 404 (Not Found)` . – Srikanta Sethy Oct 18 '22 at 16:30
  • @SrikantaSethy, Ok change last line from `RewriteRule ^(omm)/([^/]*)/?$ $1/cab-details.php?v_name=$2 [QSA,NC,L]` **to** `RewriteRule ^(omm)/([^/]*)/?$ /$1/cab-details.php?v_name=$2 [QSA,NC,L]` once and let me know how it goes. – RavinderSingh13 Oct 18 '22 at 16:31
  • After Update .htaccess code and clear browser cache. Then run-in browser i got url `http://localhost/omm/cab-details/ac-honda-amaze` In browser show `The requested URL was not found on this server.` In browser console `Failed to load resource: the server responded with a status of 404 (Not Found)` – Srikanta Sethy Oct 18 '22 at 16:48
  • @SrikantaSethy, ok let's try one more thing. Add line `RewriteBase /omn/` after 1st of this shown .htaccess file. Let me know how it goes, fingers – RavinderSingh13 Oct 18 '22 at 16:50
  • update `RewriteBase /omm/` . Then Same Problem – Srikanta Sethy Oct 18 '22 at 16:56
  • @SrikantaSethy, could you please once let me know complete path of htaccess omm and your php file. Like this: `/root/singh/test/.htaccess` etc – RavinderSingh13 Oct 18 '22 at 16:58
  • This is my xamp server location of .htaccess `D:\xamp\htdocs\omm` In Vs Code Editor path `D:\xamp\htdocs\omm\.htaccess` – Srikanta Sethy Oct 18 '22 at 17:01
  • All PHP file also here – Srikanta Sethy Oct 18 '22 at 17:02
  • @SrikantaSethy, I feel we are very close path is the only issue I could see. Is hraccess path is `D:\xamp\htdocs\.htaccess`? – RavinderSingh13 Oct 18 '22 at 17:02
  • .htaccess path of my project `D:\xamp\htdocs\omm\.htaccess` . Here **omm** is my project name – Srikanta Sethy Oct 18 '22 at 17:05
  • @SrikantaSethy, Ok please give a shot to my updated htaccess rules and let me know how it goes? Cheers. – RavinderSingh13 Oct 18 '22 at 17:06
  • Same Error `http://localhost/omm/cab-details/ac-honda-amaze` In browser show `The requested URL was not found on this server` . In browser console `Failed to load resource: the server responded with a status of 404 (Not Found)` – Srikanta Sethy Oct 18 '22 at 17:11
  • @SrikantaSethy, are you not hitting `http://localhost/projectname/cab-details.php?v_name=variable` like this link in browser? If not then these rules wouldn't work as I already stated in previous comments. These rules look for links like(example) `http://localhost/projectname/cab-details.php?v_name=variable` only. – RavinderSingh13 Oct 18 '22 at 17:13
  • Yes, I hitting `http://localhost/omm/cab-details.php?v_name=ac-swift-dzire` . I see at time hitting button mouse hover link has shown in browser bottom. – Srikanta Sethy Oct 18 '22 at 17:18
  • @SrikantaSethy, So means URL `http://localhost/omm/cab-details.php?v_name=ac-swift-dzire` is going successfully redirect to url `http://localhost/omm/cab-details/ac-swift-dzire` but its giving 404 error. Ok 1 more thing your .htaccess and cab-details.php both are present inside omm folder right like this: `D:\xamp\htdocs\omm\.htaccess` **and** `D:\xamp\htdocs\omm\cab-details.php` correct? – RavinderSingh13 Oct 18 '22 at 17:22
  • Yes, you are right... – Srikanta Sethy Oct 18 '22 at 17:25
  • @SrikantaSethy, ok then my next check would be, can you check if you have %{DOCUMENT_ROOT} variable value set to `D:\xamp\htdocs` in your apache conf file? Because if its not set then it will cause issues. Let me know. – RavinderSingh13 Oct 18 '22 at 17:26
  • In apache conf `ServerRoot "D:/xamp/apache"` . Are you exactly discus about this. – Srikanta Sethy Oct 18 '22 at 17:32
  • @SrikantaSethy, you mean %{DOCUMENT_ROOT} is set to `D:\xamp\htdocs` in conf file, sorry I didn't get your last comment. – RavinderSingh13 Oct 18 '22 at 17:34
  • @SrikantaSethy, Could you please do let me know if you have checked %{DOCUMENT_ROOT} and how it went? – RavinderSingh13 Oct 19 '22 at 02:30